~ chicken-core (chicken-5) 118fcebbb7fcaf85aa2842806d3f5ee8fb2d14b6
commit 118fcebbb7fcaf85aa2842806d3f5ee8fb2d14b6
Author: Peter Bex <peter@more-magic.net>
AuthorDate: Tue Oct 10 17:29:44 2017 +0200
Commit: Kooda <kooda@upyum.com>
CommitDate: Sun Oct 22 13:49:09 2017 +0200
Threads are tricky business. We must sacrifice a goat in the name of the dark gods to make them work properly!
When scheduling a new thread, we need to invoke it with a continuation
that goes nowhere. We know the thread call won't return, so the
continuation must be swapped out because the scheduler continuation
still has a reference to the old thread before invocation. This
continuation itself still may holds a reference to the previous
thread, and so on, resulting in infinite memory buildup.
This fixes #1367
Signed-off-by: Kooda <kooda@upyum.com>
diff --git a/NEWS b/NEWS
index 2321084c..397fd532 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,9 @@
- Runtime system:
- The profiler no longer uses malloc from a signal handler which may
cause deadlocks (#1414, thanks to Lemonboy).
+ - The scheduler no longer indirectly hangs on to the old thread
+ when switching to a new one, which caused excessive memory
+ consumption (#1367, thanks to "megane").
- Syntax expander
- Renaming an identifier twice no longer results in an undo of the
diff --git a/scheduler.scm b/scheduler.scm
index 0532048d..0b292f7f 100644
--- a/scheduler.scm
+++ b/scheduler.scm
@@ -158,7 +158,9 @@ EOF
(##sys#restore-thread-state-buffer thread)
;;XXX WRONG! this sets the t/i-period ("quantum") for the _next_ thread
(##core#inline "C_set_initial_timer_interrupt_period" (##sys#slot thread 9))
- ((##sys#slot thread 1)) )
+ ;; Call upon ye ancient gods to forget about the current
+ ;; continuation; it still refers to the old thread (#1367).
+ (##sys#call-with-cthulhu (##sys#slot thread 1)) )
(let* ([ct ##sys#current-thread]
[eintr #f]
[cts (##sys#slot ct 3)] )
Trap