~ chicken-core (master) 59c2e7a55747f04db7999561c3fda77492655f60
commit 59c2e7a55747f04db7999561c3fda77492655f60
Author: Peter Bex <peter@more-magic.net>
AuthorDate: Wed Oct 28 09:02:40 2026 +0100
Commit: felix <felix@call-with-current-continuation.org>
CommitDate: Wed Oct 28 11:00:26 2026 +0100
Restrict closure sharing to internal procedures
Unfortunately, even non-escaping procedures are not safe to reuse,
because multiple threads may re-invoke the same continuation, causing
threads to stomp on each other's closure variables.
By restricting this to internal procedures only, these should never be
re-invoked across threads because they are not reachable by user code,
because no reference to them exists. Each thread should invoke a
common "reachable" continuation and then these may create new closure
containers that are never shared between threads.
Note that this thankfully still retains the main reason for creating
shared closures - to avoid excessive copying of closure objects when
calling functions with many non-static arguments.
Reported by Ivan Raikov
Signed-off-by: felix <felix@call-with-current-continuation.org>
diff --git a/NEWS b/NEWS
index c2e98609..0e5f4b2d 100644
--- a/NEWS
+++ b/NEWS
@@ -99,6 +99,8 @@
rewrites, added rewrites for R7RS bytevector operations (thanks
to Massimo Nocentini).
- Refuse block compilation while emitting import libs (reported by "Juan").
+ - Restrict shared closures to "internal" procedures, to avoid state
+ corruption in the presence of threads (reported by Ivan Raikov).
6.0.0
diff --git a/core.scm b/core.scm
index c33e33a3..4d9d7e62 100644
--- a/core.scm
+++ b/core.scm
@@ -2436,28 +2436,29 @@
(db-put! db (first lparams) 'explicit-rest #t)
(db-put! db rest 'consed-rest-arg #t) ) ) ) ) ) ) ) ) )
- ;; If it has a known or local value which is a procedure, and referenced only once
- ;; and only one call site or is an internal procedure, mark it as 'shareable-user so that
- ;; its closed over variables may be shared with its containing procedure.
+ ;; If it has a known or local value which is a procedure, and is an internal procedure,
+ ;; mark it as 'shareable-user so that its closed over variables may be shared with its
+ ;; containing procedure.
+ ;;
+ ;; It would also work for non-escaping procedures in general, but different threads may
+ ;; re-invoke the same continuation at different times, which means internal values may
+ ;; get clobbered when a different thread runs, so we can do this for internal procedures
+ ;; only, as those never get shared between threads.
+ ;;
;; Note that callbacks are exempt from this, because callback_wrapper creates an empty closure
;; manually, throwing away our carefully crafted closure. TODO: can maybe be done better?
;;
;; If furthermore it only contains a single other procedure, mark it as 'shareable-container
- ;; so that may share closed-over variables with that one procedure.
+ ;; so that it may share closed-over variables with that one procedure.
(and-let* ((val (or local-value value))
((eq? '##core#lambda (node-class val)))
(lparams (node-parameters val))
- ((or (= 1 nreferences ncall-sites)
- (not (second lparams))))
+ ((not (second lparams)))
((not (rassoc sym callback-names eq?))))
(db-put! db (first lparams) 'shareable-user #t)
(and-let* ((id (first lparams))
(contains (or (db-get db id 'contains) '()))
((= (length contains) 1)))
- ;; TODO: It should be possible to have escaping / global procedures be containers, but
- ;; they should not call themselves because then they might be setting variables in
- ;; the closure to different values at different times. So for now we're extra careful
- ;; about which are containers.
(db-put! db (first lparams) 'shareable-container #t)))
;; Make 'removable, if it has no references and is not assigned to, and one of the following:
Trap