-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: Support interrupted shutdown hooks in multithreaded appl…
…ication (#3850) * Implement `JoinNonDeamonThreads` as method called by main thread injected by codegen instead of shutdown hook * Prevent deadlocks when executing shutdown hooks on SIGTERM/SIGINT * Add scripted tests for interuption of threads executiuon * Supress showing InterruptedExceptions when building * Don't run shutdown test on Windows, due to deadlocks in the GC - cannot be fixed currently
- Loading branch information
1 parent
8ac5d27
commit 8c49efa
Showing
13 changed files
with
183 additions
and
54 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
29 changes: 0 additions & 29 deletions
29
javalib/src/main/scala/scala/scalanative/runtime/JoinNonDeamonThreads.scala
This file was deleted.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
nativelib/src/main/scala/scala/scalanative/runtime/JoinNonDeamonThreads.scala
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package scala.scalanative.runtime | ||
|
||
import NativeThread.Registry | ||
import scala.scalanative.meta.LinktimeInfo.isMultithreadingEnabled | ||
|
||
private[runtime] object JoinNonDaemonThreads { | ||
private var shouldWait = true | ||
def skip(): Unit = shouldWait = false | ||
|
||
def run(): Unit = if (isMultithreadingEnabled) if (shouldWait) { | ||
def pollNonDaemonThreads = Registry.aliveThreads.iterator | ||
.map(_.thread) | ||
.filter { thread => | ||
thread != Thread.currentThread() && !thread.isDaemon() && | ||
thread.isAlive() | ||
} | ||
|
||
Registry.onMainThreadTermination() | ||
Iterator | ||
.continually(pollNonDaemonThreads) | ||
.takeWhile(_.hasNext && shouldWait) | ||
.flatten | ||
.foreach(_.join()) | ||
} | ||
} |
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 +1,5 @@ | ||
> runTest | ||
$ copy-file variants/SetDeleteOnExit.scala src/main/scala/Main.scala | ||
> runTestDeleteOnExit | ||
|
||
$ copy-file variants/ThreadsJoin.scala src/main/scala/Main.scala | ||
> runTestThreadsJoin |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import scala.util.Random | ||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val joinThreads = args.contains("--join") | ||
val threads = List | ||
.tabulate(8) { id => | ||
new Thread(() => { | ||
sys.addShutdownHook(println(s"On shutdown:$id")) | ||
while (true) { | ||
Thread.sleep(100 + Random.nextInt(1000)) | ||
print(s"$id;") | ||
} | ||
}) | ||
} | ||
threads.foreach(_.start()) | ||
if (joinThreads) threads.foreach(_.join()) | ||
} | ||
} |
Oops, something went wrong.