"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Gracefully Shut Down an HTTP Server After Returning a Response?

How to Gracefully Shut Down an HTTP Server After Returning a Response?

Published on 2024-11-07
Browse:692

How to Gracefully Shut Down an HTTP Server After Returning a Response?

Shutting down HTTP server after returning response

You are developing a command-line bot that interacts with the Instagram API using OAuth, which is not optimized for command-line applications. To overcome this, you have set up a local HTTP server as the redirect URI for authorization. After the user authorizes the application and is redirected to the server, you'd like to shut down the server once the access token has been displayed.

You have encountered an issue when implementing the shutdown mechanism. Specifically, calling srv.Shutdown(nil) in the request handler (showTokenToUser) leads to an error:

2017/11/23 16:02:03 Httpserver: ListenAndServe() error: http: Server closed
2017/11/23 16:02:03 http: panic serving [::1]:61793: runtime error: invalid memory address or nil pointer dereference

Solution

The issue arises because you are calling srv.Shutdown(nil) multiple times:

  1. In the showTokenToUser handler, where it's located within a closure.
  2. After the handler finishes execution in the main function.

Calling srv.Shutdown while the server is still listening for connections leads to a race condition. The ListenAndServe() goroutine attempts to close the open listeners and idle connections, but it's interrupted by the subsequent call to Shutdown in the handler closure. This state inconsistency triggers the panic.

To resolve this issue, you can use one of two methods:

1. Use context.WithCancel:

In this approach, you create a context.Context with a cancel function. The context is passed to the ListenAndServe goroutine and the showTokenToUser handler function. Inside the handler, when the access token has been displayed to the user, you call the cancel function to terminate the context. The ListenAndServe goroutine will gracefully shut down the server when the context is canceled.

2. Use the same Context:

Instead of calling context.WithCancel, you can pass the same context.Context to the ListenAndServe goroutine and the handler function. When the access token is displayed, you call cancel() on the context, which will trigger the shutdown of both the handler and the ListenAndServe goroutine.

After implementing either approach, remember to wait for the srv.Shutdown function to complete before exiting the program.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3