Skip to content

Commit dfe645a

Browse files
authored
Merge pull request #573 from nickelser/forkable-timer-set
TimerSet now survives a fork.
2 parents 6c0bbb4 + 4469174 commit dfe645a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lib/concurrent/executor/timer_set.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def ns_initialize(opts)
7878
@task_executor = Options.executor_from_options(opts) || Concurrent.global_io_executor
7979
@timer_executor = SingleThreadExecutor.new
8080
@condition = Event.new
81+
@ruby_pid = $$ # detects if Ruby has forked
8182
self.auto_terminate = opts.fetch(:auto_terminate, true)
8283
end
8384

@@ -95,6 +96,7 @@ def post_task(task)
9596
# @!visibility private
9697
def ns_post_task(task)
9798
return false unless ns_running?
99+
ns_reset_if_forked
98100
if (task.initial_delay) <= 0.01
99101
task.executor.post{ task.process_task }
100102
else
@@ -121,11 +123,20 @@ def remove_task(task)
121123
#
122124
# @!visibility private
123125
def ns_shutdown_execution
126+
ns_reset_if_forked
124127
@queue.clear
125128
@timer_executor.kill
126129
stopped_event.set
127130
end
128131

132+
def ns_reset_if_forked
133+
if $$ != @ruby_pid
134+
@queue.clear
135+
@condition.reset
136+
@ruby_pid = $$
137+
end
138+
end
139+
129140
# Run a loop and execute tasks in the scheduled order and at the approximate
130141
# scheduled time. If no tasks remain the thread will exit gracefully so that
131142
# garbage collection can occur. If there are no ready tasks it will sleep

0 commit comments

Comments
 (0)