Skip to content

Commit 1474fbf

Browse files
Fork children from a new thread. (#63)
1 parent cb5a852 commit 1474fbf

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

lib/async/container/forked.rb

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,25 @@ def exec(*arguments, ready: true, **options)
101101
def self.fork(**options)
102102
# $stderr.puts fork: caller
103103
self.new(**options) do |process|
104-
::Process.fork do
105-
# We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
106-
Signal.trap(:INT){::Thread.current.raise(Interrupt)}
107-
Signal.trap(:TERM){::Thread.current.raise(Interrupt)} # Same as SIGINT.
108-
Signal.trap(:HUP){::Thread.current.raise(Restart)}
109-
110-
# This could be a configuration option:
111-
::Thread.handle_interrupt(SignalException => :immediate) do
112-
yield Instance.for(process)
113-
rescue Interrupt
114-
# Graceful exit.
115-
rescue Exception => error
116-
Console.error(self, error)
104+
::Thread.new do
105+
::Process.fork do
106+
# We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
107+
Signal.trap(:INT){::Thread.current.raise(Interrupt)}
108+
Signal.trap(:TERM){::Thread.current.raise(Interrupt)} # Same as SIGINT.
109+
Signal.trap(:HUP){::Thread.current.raise(Restart)}
117110

118-
exit!(1)
111+
# This could be a configuration option:
112+
::Thread.handle_interrupt(SignalException => :immediate) do
113+
yield Instance.for(process)
114+
rescue Interrupt
115+
# Graceful exit.
116+
rescue Exception => error
117+
Console.error(self, error)
118+
119+
exit!(1)
120+
end
119121
end
120-
end
122+
end.value
121123
end
122124
end
123125

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Forked containers now fork child processes from a short-lived thread, reducing inherited scheduler and parent stack state in children.
6+
37
## v0.35.1
48

59
- **Fixed**: `Hybrid` container now stops on interrupt instead of restarting indefinitely.

0 commit comments

Comments
 (0)