-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BackgroundHelper: avoid leaking threads (#475)
* Fix activerecord trilogy adapter config override test * BackgroundHelper: avoid leaking threads Many test cases would define `teardown` without calling super so we were never killing threads... It's better to use `before_teardown` anyway, and it's less popular so less likely to be overriden without a super. This is very likely to be the cause of the crash we've seen at https://github.com/Shopify/semian/actions/runs/4122969863/jobs/7120418231 Co-Authored-By: Adrianna Chang <adrianna.chang@shopify.com> * Rescue timeout errors from background thread --------- Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com> Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
- Loading branch information
1 parent
2b8c8d8
commit 154f31b
Showing
7 changed files
with
36 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module BackgroundHelper | ||
attr_writer :threads | ||
def after_setup | ||
@_threads = [] | ||
end | ||
|
||
def teardown | ||
threads.each(&:kill) | ||
self.threads = [] | ||
def before_teardown | ||
@_threads.each(&:kill) | ||
@_threads.each(&:join) | ||
@_threads = nil | ||
end | ||
|
||
private | ||
|
||
def background(&block) | ||
thread = Thread.new(&block) | ||
thread.report_on_exception = false | ||
threads << thread | ||
@_threads << thread | ||
thread.join(0.1) | ||
thread | ||
end | ||
|
||
def threads | ||
@threads ||= [] | ||
end | ||
|
||
def yield_to_background | ||
threads.each(&:join) | ||
@_threads.each(&:join) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,6 @@ | |
|
||
module Minitest | ||
class Test | ||
include BackgroundHelper | ||
include TimeHelper | ||
include ResourceHelper | ||
end | ||
|