fix: persistent bot thread to eliminate ORC heap corruption #97

Closed
opened 2026-08-27 22:45:20 +02:00 by SirStone · 1 comment
Owner

Problem

A new bot thread is created every round via createThread/joinThread. When thread N dies, its ORC allocator is destroyed, but module-level seq/string globals still reference its heap blocks. Thread N+1 touching those globals causes SIGSEGV in prepareSeqAddUninit/rawDealloc.

Current workarounds use fixed-size arrays with silent ceilings:

  • Event queue: 256 events/tick
  • Team messages: 16/tick
  • Stdout/stderr: 4096 bytes/tick
  • SVG graphics: 16384 bytes/tick

Solution

Replace thread-per-round with a single persistent bot thread that lives for the process lifetime. Between rounds it blocks on a channel waiting for the next round-start signal. This keeps the allocator alive, eliminating all fixed-array workarounds.

Files

  • src/robocode_tankroyale_botapi/bot.nim (thread lifecycle, fixed arrays)
  • src/robocode_tankroyale_botapi/event_queue.nim (fixed array)
  • src/robocode_tankroyale_botapi/graphics.nim (fixed array)
## Problem A new bot thread is created every round via `createThread`/`joinThread`. When thread N dies, its ORC allocator is destroyed, but module-level `seq`/`string` globals still reference its heap blocks. Thread N+1 touching those globals causes SIGSEGV in `prepareSeqAddUninit`/`rawDealloc`. Current workarounds use fixed-size arrays with silent ceilings: - Event queue: 256 events/tick - Team messages: 16/tick - Stdout/stderr: 4096 bytes/tick - SVG graphics: 16384 bytes/tick ## Solution Replace thread-per-round with a single persistent bot thread that lives for the process lifetime. Between rounds it blocks on a channel waiting for the next round-start signal. This keeps the allocator alive, eliminating all fixed-array workarounds. ## Files - `src/robocode_tankroyale_botapi/bot.nim` (thread lifecycle, fixed arrays) - `src/robocode_tankroyale_botapi/event_queue.nim` (fixed array) - `src/robocode_tankroyale_botapi/graphics.nim` (fixed array)
Author
Owner

Fixed in v1.0.2. Bot thread is now persistent across rounds — created once, waits on a channel between rounds. All fixed-array workarounds removed, normal seq/string restored. Validated with 3-round battle test.

Fixed in v1.0.2. Bot thread is now persistent across rounds — created once, waits on a channel between rounds. All fixed-array workarounds removed, normal seq/string restored. Validated with 3-round battle test.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: SirStone/SirRoboGarage#97