Aller au contenu

Thread without join (ARCH6041)

Ce contenu n’est pas encore disponible dans votre langue.

Key ruby.async.thread-without-join · Severity warning · Category async

A thread started without a matching join lets the spawning scope exit before the thread finishes. Its work may be cut short and exceptions raised inside it are silently lost.

def process_batch(items)
items.each do |item|
Thread.new { upload(item) }
end
# method returns; uploads may not finish, errors lost
end
def process_batch(items)
threads = items.map do |item|
Thread.new { upload(item) }
end
threads.each(&:join)
end

Keep each spawned thread in a variable and call join on it before leaving the scope.