Skip to content

TimerSet now survives a fork. #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/concurrent/executor/timer_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def ns_initialize(opts)
@task_executor = Options.executor_from_options(opts) || Concurrent.global_io_executor
@timer_executor = SingleThreadExecutor.new
@condition = Event.new
@ruby_pid = $$ # detects if Ruby has forked
self.auto_terminate = opts.fetch(:auto_terminate, true)
end

Expand All @@ -95,6 +96,7 @@ def post_task(task)
# @!visibility private
def ns_post_task(task)
return false unless ns_running?
ns_reset_if_forked
if (task.initial_delay) <= 0.01
task.executor.post{ task.process_task }
else
Expand All @@ -121,11 +123,20 @@ def remove_task(task)
#
# @!visibility private
def ns_shutdown_execution
ns_reset_if_forked
@queue.clear
@timer_executor.kill
stopped_event.set
end

def ns_reset_if_forked
if $$ != @ruby_pid
@queue.clear
@condition.reset
@ruby_pid = $$
end
end

# Run a loop and execute tasks in the scheduled order and at the approximate
# scheduled time. If no tasks remain the thread will exit gracefully so that
# garbage collection can occur. If there are no ready tasks it will sleep
Expand Down