You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: context/getting-started.md
+42-2Lines changed: 42 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,7 +139,7 @@ The installed handlers are snapshotted when they are installed. Later changes to
139
139
140
140
### Choosing a Signal Backend
141
141
142
-
Use {ruby Async::Signals.default} when a component should install process signal handlers only while running on the main thread. It returns {ruby Async::Signals} on the main thread and {ruby Async::Signals::Ignore} on other threads.
142
+
Use {ruby Async::Signals.default} when a component should install process signal handlers only when it appears to own the process signal boundary. It returns {ruby Async::Signals} on the main thread when no fiber scheduler is installed, and {ruby Async::Signals::Ignore} otherwise.
143
143
144
144
```ruby
145
145
require"async/signals"
@@ -150,13 +150,53 @@ handlers.trap(:TERM) do
150
150
end
151
151
152
152
Async::Signals.default.install(handlers) do
153
-
# Process signal handlers are active only on the main thread.
153
+
# Process signal handlers are active only when using the default signal backend.
154
154
sleep
155
155
end
156
156
```
157
157
158
158
Use {ruby Async::Signals::Ignore} directly when a component is controlled by its parent and should not subscribe to process-wide signals.
159
159
160
+
### Using Signals with Async
161
+
162
+
When a component runs inside an existing Async event loop, it should not implicitly take ownership of process-wide signals. In that case, {ruby Async::Signals.default} returns {ruby Async::Signals::Ignore}, so installing handlers through the default backend becomes a no-op.
163
+
164
+
```ruby
165
+
require"async"
166
+
require"async/signals"
167
+
168
+
handlers =Async::Signals::Handlers.new
169
+
handlers.trap(:TERM) do
170
+
puts"Stopping..."
171
+
end
172
+
173
+
Asyncdo
174
+
Async::Signals.default.install(handlers) do
175
+
# No process signal traps are installed here.
176
+
sleep
177
+
end
178
+
end
179
+
```
180
+
181
+
If a component running inside an Async event loop is intended to own process signal handling, pass {ruby Async::Signals} explicitly instead of using the default backend.
182
+
183
+
```ruby
184
+
require"async"
185
+
require"async/signals"
186
+
187
+
handlers =Async::Signals::Handlers.new
188
+
handlers.trap(:TERM) do |signal, context|
189
+
context.raise(Interrupt)
190
+
end
191
+
192
+
Asyncdo
193
+
Async::Signals.install(handlers) do
194
+
# Process signal traps are explicitly installed here.
195
+
sleep
196
+
end
197
+
end
198
+
```
199
+
160
200
## Forking
161
201
162
202
Signal traps are inherited across `fork`. On Ruby implementations that support `Process._fork`, `async-signals` automatically resets inherited signal state in the forked child so the child does not keep handler registrations from the parent process.
0 commit comments