Skip to content

Commit ddb5f75

Browse files
committed
Fix shutdown of periodic_updater
* current implementation produces "error: can't be called from trap"
1 parent 70105a4 commit ddb5f75

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/cloud_controller/runners/puma_runner.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ 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 do
66-
@periodic_updater.stop_updates unless @periodic_updater.nil?
67-
end
65+
events.after_stopped(&method(:shutdown_periodic_updater))
6866

6967
@puma_launcher = Puma::Launcher.new(puma_config, log_writer:, events:)
7068
end
@@ -78,6 +76,21 @@ def start!
7876

7977
private
8078

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
88+
end
89+
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")}"
92+
end
93+
8194
def prometheus_updater
8295
CloudController::DependencyLocator.instance.prometheus_updater
8396
end

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

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

210210
puma_launcher.events.fire(:after_stopped)
211+
sleep 0.1
211212
end
212213
end
213214
end

0 commit comments

Comments
 (0)