Skip to content

feat(tty): serialize terminal output writes with a lock#31

Merged
tonyfettes merged 4 commits into
mainfrom
feat/output-write-lock
Jun 15, 2026
Merged

feat(tty): serialize terminal output writes with a lock#31
tonyfettes merged 4 commits into
mainfrom
feat/output-write-lock

Conversation

@tonyfettes

Copy link
Copy Markdown
Contributor

What

Output writes on Tty are now serialized through an internal lock, so a single Tty::write / Tty::write_string call lands on the terminal atomically even when multiple tasks write concurrently. Previously a large write could span multiple write_once syscalls with suspension points in between, letting another writer interleave its bytes and garble escape sequences / frames.

Changes

  • New internal package moonbit-community/tty/internal/lock wrapping a binary semaphore (Semaphore(1)):
    • Lock::new / acquire / try_acquire / release
    • with_lock(action) pairs acquire/release via defer, releasing on both the normal and failure paths (defer is registered only after acquire succeeds, so a cancelled acquire never releases a lock it doesn't hold).
  • Tty gains a write_lock : @lock.Lock field (both Windows and non-Windows variants); write / write_string route through write_lock.with_lock(...).
  • Upgrade moonbitlang/async 0.19.10.19.4 for the semaphore API.
  • Tests for serialization (concurrent critical sections don't overlap, even across suspension) and release-on-failure.

Scope / notes

  • Every byte written to the terminal output stream goes through write / write_string (all of style.mbt / decstbm.mbt command methods and the query_* helpers route here), so all output-stream writes are covered. size.mbt / state.mbt only touch self.output.fd() for ioctls (tcgetattr/tcsetattr/window size), which are independent atomic syscalls and intentionally not locked.
  • Locking granularity is per call. A method that issues several writes (e.g. enable_mouse) is a sequence of atomic writes, not one atomic unit — another writer can still interleave between them. Frame-level atomicity requires assembling one buffer per write.

Validation

moon check / moon test (165 passed) / moon fmt / moon info all clean.

🤖 Generated with Claude Code

tonyfettes and others added 3 commits June 15, 2026 10:54
Add an internal `lock` package wrapping a binary semaphore (Semaphore(1))
and route Tty::write / Tty::write_string through it, so a single write
lands on the terminal atomically even when multiple tasks write
concurrently. with_lock pairs acquire/release via defer, releasing on
both the normal and failure paths.

Upgrade moonbitlang/async 0.19.1 -> 0.19.4 for the semaphore API.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The win32 console white-box test builds a Tty record literal directly,
which must include the new write_lock field. Fixes the windows-latest CI
check.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The Semaphore API the write lock needs already exists in 0.19.1 with an
identical signature, so the bump to 0.19.4 was unnecessary. 0.19.4 also
changed Windows-affecting internals and regressed the Tty write path on
windows-latest (Tty::open test timed out). Staying on 0.19.1 keeps the
lock feature while matching the version the tests module already pins.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a4e78605bc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tty.mbt
Add docs/plans/2026-06-15-output-write-lock.md with goal, target files,
public API changes, invariants, acceptance criteria, validation, and the
public API audit notes required when .mbti files change, and add the
TTY-23 row to the execution board. Addresses the plan-discipline review.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@tonyfettes tonyfettes merged commit 0322bd5 into main Jun 15, 2026
3 checks passed
@tonyfettes tonyfettes deleted the feat/output-write-lock branch June 15, 2026 03:19
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