Skip to content

Commit dd61383

Browse files
committed
Use at_exit hook to shut down periodic_updater
* avoids use of sleep commands
1 parent ddb5f75 commit dd61383

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

lib/cloud_controller/runners/puma_runner.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ def initialize(config, app, logger, periodic_updater, request_logs)
6262
prometheus_updater.update_gauge_metric(:cc_db_connection_pool_timeouts_total, 0, labels: { process_type: 'main' })
6363
@periodic_updater.setup_updates
6464
end
65-
events.after_stopped(&method(:shutdown_periodic_updater))
65+
66+
events.after_stopped do
67+
stop_periodic_updates
68+
end
6669

6770
@puma_launcher = Puma::Launcher.new(puma_config, log_writer:, events:)
6871
end
@@ -76,19 +79,16 @@ def start!
7679

7780
private
7881

79-
def shutdown_periodic_updater
80-
if @periodic_updater
81-
Thread.new do
82-
# Give the trap handler a moment to return before taking any locks.
83-
sleep 0.05
84-
@periodic_updater.stop_updates
85-
rescue StandardError => e
86-
@logger.error "Failed to stop periodic updates cleanly: #{e}\n#{e.backtrace&.join("\n")}"
87-
end
82+
def stop_periodic_updates
83+
@periodic_updater&.stop_updates
84+
@logger.info('Successfully stopped periodic updates in after_stopped')
85+
rescue ThreadError
86+
at_exit do
87+
@periodic_updater&.stop_updates
88+
@logger.info('Successfully stopped periodic updates in at_exit')
8889
end
8990
rescue StandardError => e
90-
# Never let exceptions from the trap context bubble up and crash shutdown.
91-
@logger.error "Failed to schedule periodic updates shutdown: #{e}\n#{e.backtrace&.join("\n")}"
91+
@logger.error("Failed to stop periodic updates: #{e}\n#{e.backtrace&.join("\n")}")
9292
end
9393

9494
def prometheus_updater

spec/unit/lib/cloud_controller/runners/puma_runner_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ module VCAP::CloudController
208208
expect(periodic_updater).to receive(:stop_updates)
209209

210210
puma_launcher.events.fire(:after_stopped)
211-
sleep 0.1
212211
end
213212
end
214213
end

0 commit comments

Comments
 (0)