Skip to content

deps: adopt panic-safe golua fork and absorb its consequences - #10

Open
srgg wants to merge 1 commit into
mainfrom
deps/panic-safe-golua-fork
Open

deps: adopt panic-safe golua fork and absorb its consequences#10
srgg wants to merge 1 commit into
mainfrom
deps/panic-safe-golua-fork

Conversation

@srgg

@srgg srgg commented Jul 11, 2026

Copy link
Copy Markdown
Owner

What

Switches blim to the golua fork (github.com/srgg/golua) that fixes two long-standing defects this codebase worked around, and absorbs the consequences. The fork's changes are upstreamed as aarzilli/golua#130 (thread-correct dispatch) and aarzilli/golua#131 (panic containment at the cgo boundary; also fixes upstream #24).

Why

  • Go panics (RaiseError or accidental) used to unwind across in-flight lua_pcall/lua_resume C frames and corrupt the VM — the root of issue SIGSEGV when a synchronous BLE read is called from inside a subscription callback #3(b) and the disabled GC-at-close workaround (audit L-1).
  • Go functions called from coroutines ran on the main state's stack (blocked any coroutine-based scripting; a prerequisite for the RFC-002 scheduler).

Changes

  • go.mod/go.sum: replace => github.com/srgg/golua@<pseudo-version> (public fork; CI resolves it). Reverts to upstream once #130/#131 merge.
  • LuaEngine.Close (L-1): re-enable the full-GC-then-stop sequence before lua_close — the "GC moved crash earlier" corruption was the panic-crossing class, now fixed; finalizers run against an intact registry again.
  • blim.lua: restore standard pcall/xpcall globals (hidden by golua because Go panics bypassed them unsafely); blim.pcall becomes a deprecated alias. Go-backed errors are now catchable and the VM stays intact afterwards.
  • Comment hygiene (api.go, lua_engine.go): rewrite ~14 stale "a Go panic corrupts the recovered lua_State / not catchable by pcall" rationales — keep the still-valid mutex-reentrancy and self-deadlock reasoning, move the fixed golua class to HISTORY notes; correct the cancellation comment; remove two no-op inner recover blocks in the dispatchers.
  • README.md: pcall section no longer claims Go-backed errors "abort by design"; documents standard pcall/xpcall and the catchable-cancellation caveat.

Scope note

The in-callback guards stay — they defend the mutex-reentrancy vector (#3(a)), which is a blim architecture constraint independent of golua and is removed only when the RFC-002 actor lands. This PR is dependency adoption + its direct consequences, not the scheduler.

Tests

  • TestGoluaDependencyContract — canary-guards the fork dependency through blim-domain behavior (blim.* inside user coroutines; script errors do not degrade the engine); fails loudly if replace is dropped to stock golua (verified: it fails with "error in error handling" on stock).
  • TestProtectedCall — adds the restored-globals case and inverts the old "Go-backed error aborts the script" pin to "caught like any Lua error".

The exhaustive golua mechanism tests live in the fork and are deliberately not duplicated here.

Full suite green (-tags "test luajit", incl. -race).

Switch to the golua fork (github.com/srgg/golua; upstream PRs
aarzilli/golua#130 thread-correct dispatch, #131 panic containment) that
fixes two long-standing defects blim worked around:

- Go panics (RaiseError, accidental) no longer unwind across in-flight
  lua_pcall/lua_resume frames and corrupt the VM;
- Go functions called from coroutines run on the correct stack.

Consequences absorbed here (all behind the same shared lua_State — the
mutex-reentrancy vector is unchanged and its in-callback guards stay until
the RFC-002 actor lands):

- Re-enable the GC-at-close sequence in LuaEngine.Close (audit L-1): the
  "GC moved crash earlier" corruption was the panic-crossing class, now
  fixed; close-time finalizers run against an intact registry again.
- Restore standard pcall/xpcall globals in blim.lua (they were hidden
  because Go panics bypassed them unsafely); blim.pcall becomes a deprecated
  alias. Go-backed errors are now catchable and the VM stays intact.
- Comment hygiene: rewrite the ~14 stale "a Go panic corrupts the recovered
  lua_State / bypasses the VM unwinder / not catchable by pcall" rationales;
  keep the still-valid mutex-reentrancy and self-deadlock reasoning, move the
  fixed golua class to HISTORY notes. Correct the cancellation comment (the
  raise IS catchable now; a swallowing script falls back to the grace
  timeout). Remove two no-op inner recover blocks in the callback dispatchers.
- README: the pcall section no longer claims Go-backed errors "abort by
  design"; documents standard pcall/xpcall and the catchable-cancellation
  caveat.

Tests:
- TestGoluaDependencyContract canary-guards the fork dependency through
  blim-domain behavior (blim.* inside user coroutines; script errors do not
  degrade the engine) — fails loudly if the replace is dropped to stock golua.
- TestProtectedCall gains the restored-globals case and inverts the old
  "Go-backed error aborts the script" pin to "caught like any Lua error".

The exhaustive golua mechanism tests live in the fork; they are deliberately
not duplicated here.
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