Skip to content

fix(examples): don't unref the MainLoop in glib-timeout (#429)#431

Merged
romgrk merged 1 commit into
masterfrom
fix/glib-timeout-double-unref
Jun 14, 2026
Merged

fix(examples): don't unref the MainLoop in glib-timeout (#429)#431
romgrk merged 1 commit into
masterfrom
fix/glib-timeout-double-unref

Conversation

@romgrk

@romgrk romgrk commented Jun 14, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes the crash in node examples/glib-timeout.js tracked by #429.

The example balanced new GLib.MainLoop() with loop.unref() (the C idiom). But node-gtk's boxed wrapper already owns that reference and g_main_loop_unref()s it on GC, so the explicit loop.unref() drops it a second time → double-free of the GMainLoop. Standalone that's only a g_main_loop_unref: ref_count > 0 warning, but with gi.startLoop() active the heap corruption faults during Node's loop teardown at exit (SIGSEGV in uv_close).

This removes the loop.unref() (and fixes the stale callback.js header).

Verified on Ubuntu 26.04 (rootless podman; libffi 3.5.2 / gir 1.86.0 / glib 2.88 / node 22)

crashes
before (loop.unref()) 30/30
after (this PR) 0/30

Full run is clean: Run loop. → count 0..3 → Loop ran., exit 0.

Note (separate, not in this PR)

This is a narrow fix for the example. The underlying hazard — node-gtk exposing C ref()/unref()/free() on GC-owned boxed wrappers, so following normal C ownership idioms double-frees — is left as an open design question in #429.

🤖 Generated with Claude Code

The example followed the C idiom of balancing g_main_loop_new() with
g_main_loop_unref(), but node-gtk's boxed wrapper already owns that reference
and releases it on GC. The explicit loop.unref() drops it a second time, and
the resulting double-free of the GMainLoop corrupts the heap — which, with
gi.startLoop() active, faults during node's loop teardown at process exit
(SIGSEGV in uv_close). Verified on Ubuntu 26 (libffi 3.5): 30/30 runs crash
with the unref, 0/30 without.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@romgrk romgrk merged commit ec0ab86 into master Jun 14, 2026
9 checks passed
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