Skip to content

mgs: pause without catching the game thread inside printf - #9

Merged
finger563 merged 1 commit into
mainfrom
fix/mgs-pause-stdio
Oct 24, 2026
Merged

finger563 merged 1 commit into
mainfrom
fix/mgs-pause-stdio

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Fixes a hang when the pause menu is opened while MGS is booting.

Cause

mgs::freeze() suspends the live PSX thread wherever it is. During boot the game is never idle and prints a lot, so the pause can suspend it inside printf while it holds stdout's lock. The cart's next log line (in Cart::screenshot) then waits for that lock forever.

A core dump taken by a temporary watchdog showed:

  • mgs_th suspended in printf("TASK START..."), inside uart_write
  • main blocked in __flockfile(stdout) → Cart::screenshot

Fix

The thread is suspended while the pausing task holds stdout's and stderr's locks. The game thread is then either outside stdio, or waiting for the lock, in which case it takes the lock after the resume.

Testing (Tab5)

A temporary stress hook (local only) opened the pause menu on every game's first frame, then every 4 s.

  • Before: MGS hung on its first menu open, 8 ms after the game's main().
  • After: two MGS launches opened the menu at that same point, with 150+ menu opens across 13 launches of every game and no hang.

🤖 Generated with Claude Code

freeze() suspends the live PSX thread wherever it is. While the game boots
it is not idle and prints a lot, so opening the pause menu in the first
second could suspend it inside printf, holding stdout's lock; the cart's
next log line (Cart::screenshot) then waited for it forever and the device
hung on the pause.

The thread is now suspended while this task holds stdout's and stderr's
locks, so it is either outside stdio or waiting for the lock (and takes it
after the resume).

Reproduced by opening the menu on MGS's first frame (8 ms after the game's
main()): hung, and a core dump showed mgs_th suspended in
printf("TASK START...") and main blocked in __flockfile(stdout). With the
fix, a 25 minute run opening the menu on every game's first frame and then
every 4 s did not hang.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@finger563
finger563 merged commit 1797ef9 into main Oct 24, 2026
1 check passed
@finger563
finger563 deleted the fix/mgs-pause-stdio branch October 24, 2026 07:48
@github-actions

Copy link
Copy Markdown

ESP-IDF Size Report for 'Tab5 Emu'

Metric Base PR Delta
FLASH 0 bytes (0.00%) 0 bytes (0.00%) 0 bytes (0.00%)
DRAM 267,182 bytes (60.54%) 267,182 bytes (60.54%) 0 bytes (0.00%)
IRAM 0 bytes 0 bytes 0 bytes
RAM (DRAM+IRAM) 267,182 bytes 267,182 bytes 0 bytes (0.00%)

FLASH uses app .bin size or json2 flash sum. RAM sums DRAM+IRAM via idf_size. Percentages shown when totals are available.
DRAM/IRAM usage does not include memory used by the heap allocator at runtime.
This report was generated by esp-idf-size-delta.

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