Skip to content

Commit 149bacc

Browse files
Simplify child wait methods
Assisted-By: devx/3236e566-7538-432e-a30a-2bdf37265ed4
1 parent 59805b9 commit 149bacc

2 files changed

Lines changed: 19 additions & 34 deletions

File tree

lib/async/container/forked.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,8 @@ def restart!
235235
# Wait for the child process to exit.
236236
# @asynchronous This method may block.
237237
#
238-
# @parameter timeout [Numeric | Nil] Ignored; retained for compatibility.
239238
# @returns [::Process::Status] The process exit status.
240-
def wait(timeout = nil)
239+
def wait
241240
if @pid && @status.nil?
242241
Console.debug(self, "Waiting for process to exit...", child: {process_id: @pid})
243242
_, @status = ::Process.wait2(@pid)

lib/async/container/threaded.rb

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,11 @@ def initialize(name: nil, **options)
129129
super(**options)
130130

131131
@status = nil
132+
@joined = false
132133

133134
@thread = yield(self)
134135
@thread.report_on_exception = false
135136
@thread.name = name
136-
137-
@waiter = ::Thread.new do
138-
begin
139-
@thread.join
140-
rescue Exit => exit
141-
finished(exit.error)
142-
rescue Interrupt
143-
# Graceful shutdown.
144-
finished
145-
rescue Exception => error
146-
finished(error)
147-
else
148-
finished
149-
end
150-
end
151137
end
152138

153139
# Convert the child process to a hash, suitable for serialization.
@@ -216,28 +202,28 @@ def restart!
216202
@thread.raise(Restart)
217203
end
218204

219-
# Wait for the thread to exit and return he exit status.
205+
# Wait for the thread to exit and return the exit status.
220206
# @asynchronous This method may block.
221207
#
222-
# @parameter timeout [Numeric | Nil] Maximum time to wait before forceful termination.
223208
# @returns [Status]
224-
def wait(timeout = nil)
225-
if @waiter
226-
Console.debug(self, "Waiting for thread to exit...", child: {thread_id: @thread.object_id}, timeout: timeout)
209+
def wait
210+
unless @joined
211+
Console.debug(self, "Waiting for thread to exit...", child: {thread_id: @thread.object_id})
227212

228-
if timeout
229-
unless @waiter.join(timeout)
230-
Console.warn(self, "Thread is blocking, sending kill signal...", child: {thread_id: @thread.object_id}, timeout: timeout)
231-
self.kill!
232-
@waiter.join
233-
end
213+
begin
214+
@thread.join
215+
rescue Exit => exit
216+
finished(exit.error)
217+
rescue Interrupt
218+
# Graceful shutdown.
219+
finished
220+
rescue Exception => error
221+
finished(error)
234222
else
235-
until @waiter.join(0)
236-
sleep(0.1)
237-
end
223+
finished
224+
ensure
225+
@joined = true
238226
end
239-
240-
@waiter = nil
241227
end
242228

243229
Console.debug(self, "Thread exited.", child: {thread_id: @thread.object_id, status: @status})
@@ -278,7 +264,7 @@ def to_s
278264

279265
protected
280266

281-
# Invoked by the @waiter thread to indicate the outcome of the child thread.
267+
# Record the outcome of the child thread and close the notification channel.
282268
def finished(error = nil)
283269
if error
284270
Console.error(self){error}

0 commit comments

Comments
 (0)