Skip to content

perf: Reduce PTY read latency and fix lock contention #53

@datasciencemonkey

Description

@datasciencemonkey

Problem

The current PTY read loop and session management have latency and contention issues that affect terminal responsiveness, especially under high-volume output (e.g., seq 1 10000).

Proposed Changes (cherry-pick from PR #52)

PR #52 includes performance fixes that are independent of the OpenCode fork and should be extracted:

1. select() timeout reduction

  • Current: 500ms timeout in the PTY read loop
  • Proposed: 50ms — reduces worst-case latency for output delivery by 10x

2. Lock contention in get_output_batch()

  • Current: Single global sessions_lock held for the entire operation
  • Proposed: 3-step resolve/swap/join pattern — grab reference under lock, swap buffer outside lock, join strings outside lock. Matches the pattern already used in get_output().

3. Lock contention in cleanup_stale_sessions()

  • Current: Iterates and cleans up under the global lock
  • Proposed: Snapshot session dict under lock, then iterate/cleanup outside lock

4. Poll-worker interval

  • Current: 100ms poll interval in static/poll-worker.js
  • Proposed: 50ms — improves responsiveness for HTTP polling fallback

Why separate from PR #52

These are changes to app.py and static/poll-worker.js only. They don't require the OpenCode fork, spawner app, or any other PR #52 components. Extracting them keeps the review focused and the blast radius small.

Files affected

  • app.py — select timeout, lock patterns
  • static/poll-worker.js — poll interval

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions