From c428db0f3de9e7c55cc1e7500d079a0422459b7e Mon Sep 17 00:00:00 2001 From: Ben Barnard Date: Wed, 20 Nov 2013 00:22:28 +0100 Subject: [PATCH] Shutdown WEBrick server used in installed app flow Starting and stopping WEBrick only controls whether the event loop is running, it does not start and stop listening on TCP sockets. Our WEBrick server is starting to listen when it is initialised, so we should ensure that we shut it down when we are done with it. Note that shutdown is idempotent. --- lib/google/api_client/auth/installed_app.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/google/api_client/auth/installed_app.rb b/lib/google/api_client/auth/installed_app.rb index 2edf36332ba..e6bd31666b2 100644 --- a/lib/google/api_client/auth/installed_app.rb +++ b/lib/google/api_client/auth/installed_app.rb @@ -92,9 +92,10 @@ def authorize(storage=nil) :Logger => WEBrick::Log.new(STDOUT, 0), :AccessLog => [] ) - trap("INT") { server.shutdown } - - server.mount_proc '/' do |req, res| + begin + trap("INT") { server.shutdown } + + server.mount_proc '/' do |req, res| auth.code = req.query['code'] if auth.code auth.fetch_access_token! @@ -102,10 +103,13 @@ def authorize(storage=nil) res.status = WEBrick::HTTPStatus::RC_ACCEPTED res.body = RESPONSE_BODY server.stop - end + end - Launchy.open(auth.authorization_uri.to_s) - server.start + Launchy.open(auth.authorization_uri.to_s) + server.start + ensure + server.shutdown + end if @authorization.access_token if storage.respond_to?(:write_credentials) storage.write_credentials(@authorization)