From 85a3141d2de3e2fd8e0f1dd49d3b665f2119fde2 Mon Sep 17 00:00:00 2001 From: Peter Bex 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