Hi all, After some pondering I think I know what's going on in #1367. When we enter (##sys#schedule), we simply fetch the suspended thread's continuation (stored in a thunk) from the thread structure and invoke it.
This invocation implicitly is done with the continuation that would return in the old thread from the call to (##sys#schedule). Of course, ##sys#schedule is not even _supposed_ to return, so that makes no sense. This continuation holds a reference to the code that was active at the point where we called thread-start!, which in the example program is running in the old thread (which it also keeps a reference to). Now, this example is a pathological case because all the threads do is acquire a mutex and then fire off the next thread, after which the thread is supposed to die. The next thread does the same and so on, resulting in a long chain of continuations that goes all the way back to the primordial thread. At least, I *think* that's what's going on. Running the test program with -:d shows a steady buildup of memory usage. The fix is to call the thunk with an empty continuation. We know that it won't ever be invoked, so it doesn't matter what the continuation does. And of course we have the perfect fit for that: ##sys#call-with-cthulhu! I'm not sure if I would've come up with this if it wasn't for #1408... Anyway, I hope this explanation makes sense. It's a bit difficult to explain this because it involves both the garbage collector _and_ the scheduler. Also, I'm not 100% sure about my analysis of the situation, but the patch does seem to change the memory profile of the example program to be completely steady. IMO, this patch should go into both master and chicken-5. Cheers, Peter
From 85a3141d2de3e2fd8e0f1dd49d3b665f2119fde2 Mon Sep 17 00:00:00 2001 From: Peter Bex <[email protected]> Date: Tue, 10 Oct 2017 17:29:44 +0200 Subject: [PATCH] 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 --- NEWS | 3 +++ scheduler.scm | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) 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)] ) -- 2.11.0
signature.asc
Description: PGP signature
_______________________________________________ Chicken-hackers mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/chicken-hackers
