Skip to content

Fix toplevel static initializers - #959

Open
TimWhiting wants to merge 1 commit into
koka-lang:devfrom
TimWhiting:fix-static-initializers
Open

TimWhiting wants to merge 1 commit into
koka-lang:devfrom
TimWhiting:fix-static-initializers

Conversation

@TimWhiting

Copy link
Copy Markdown
Collaborator

Top level primitives and boxed values must be marked thread-shared in order for races not to happen with reference counting. Otherwise +2 -2 references can turn into +1 -2 and free a top level static value!

@TimWhiting
TimWhiting marked this pull request as draft October 14, 2026 14:19
@TimWhiting
TimWhiting force-pushed the fix-static-initializers branch 2 times, most recently from fe3cee1 to 6aeebab Compare October 14, 2026 14:29
@TimWhiting
TimWhiting marked this pull request as ready for review October 14, 2026 14:32
@TimWhiting
TimWhiting force-pushed the fix-static-initializers branch from 6aeebab to 365d85c Compare October 14, 2026 14:48
A toplevel constant lives in a C static that every thread dups and drops, and
Perceus refcounts are not atomic: two threads updating one count race, and the
constant can be freed while still in use. `@init` now marks every block of a
constant's graph stuck (`kk_box_mark_static`): dup and drop leave it unchanged
and it is never freed, as for a compile-time static. Blocks that are already
thread-shared stay atomically counted.

`kk_box_mark_static` shares the traversal of `kk_box_mark_shared`, which now
takes whether to make blocks stuck or thread-shared.

test/cgen/static-thread-race: eight tasks dup and drop one toplevel list;
without this it segfaults on every run (5 of 5, macOS arm64).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TimWhiting
TimWhiting force-pushed the fix-static-initializers branch from 365d85c to 0612dec Compare October 27, 2026 12:44

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant