This repository was archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
fix: Use daemon threads for SolverManager #69
Merged
Christopher-Chianelli
merged 14 commits into
TimefoldAI:main
from
Christopher-Chianelli:custom-thread-factory
Jun 13, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
66e2981
feat: Use daemon threads for SolverManager
Christopher-Chianelli 1aee450
chore: Add tests for SolverManager.create, deep copy input problem
Christopher-Chianelli 6889f32
docs: Fix SolverManagerConfig docstring
Christopher-Chianelli ea54d8f
docs: Fix SolverManager.create docstring
Christopher-Chianelli 293d284
fix: Use an impossible to solve initial problem
Christopher-Chianelli ced756d
fix: Use 4 for parallel_solver_count
Christopher-Chianelli 326becf
fix: Bump parallel_solver_count to 12
Christopher-Chianelli a1f546b
fix: Remove tests
Christopher-Chianelli 0231a35
test: Use only one solver manager in test
Christopher-Chianelli 71f04f8
fix: maybe?
Christopher-Chianelli d2a9434
test: Only do problem_change_solver_run once
Christopher-Chianelli 17b79a6
fix: comment out code associated with flakey test
Christopher-Chianelli 361c499
test: remove extra solver managers
Christopher-Chianelli 0198682
chore: Add comment explaining catch-22, remove commented out code
Christopher-Chianelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
32 changes: 32 additions & 0 deletions
32
timefold-solver-python-core/src/main/java/ai/timefold/solver/python/DaemonThreadFactory.java
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ai.timefold.solver.python; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
/** | ||
* There a Catch-22 that occurs on shutdown: | ||
* <p> | ||
* - In order for Python to free its variables, it must be terminated. | ||
* - In order for Python to be terminated, the JVM must be terminated. | ||
* - In order for the JVM to be terminated, all its non-daemon threads must be terminated. | ||
* - Executors keep all its threads alive until it is freed/have no more references. | ||
* - In order for the Executor to be freed/have no more references, it cannot have a reference in Python. | ||
* - To not have a reference in Python means Python must free its variables, creating the Catch-22 | ||
* <p> | ||
* Thus, if non-daemon threads are used, and a {@link ai.timefold.solver.core.api.solver.SolverManager} | ||
* solves at least one problem (creating a keep-alive thread in its {@link java.util.concurrent.ThreadPoolExecutor}), | ||
* Python cannot shut down gracefully and will become unresponsive when interrupted. | ||
* <p> | ||
* This class uses {@link Executors#defaultThreadFactory()} to create a new thread, but sets the created | ||
* thread to daemon mode so Python can shut down gracefully. | ||
*/ | ||
public class DaemonThreadFactory implements ThreadFactory { | ||
Christopher-Chianelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final ThreadFactory THREAD_FACTORY = Executors.defaultThreadFactory(); | ||
|
||
@Override | ||
public Thread newThread(Runnable runnable) { | ||
Thread out = THREAD_FACTORY.newThread(runnable); | ||
out.setDaemon(true); | ||
return out; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.