Skip to content

fix: flush stdout after writes for real-time RPC notifications#50

Open
zerone0x wants to merge 1 commit intosteipete:mainfrom
zerone0x:fix/flush-stdout-for-rpc-notifications
Open

fix: flush stdout after writes for real-time RPC notifications#50
zerone0x wants to merge 1 commit intosteipete:mainfrom
zerone0x:fix/flush-stdout-for-rpc-notifications

Conversation

@zerone0x
Copy link

Summary

Fixes #23

Adds synchronize() call after stdout writes to prevent buffering delays in RPC notifications.

Problem

Swift's FileHandle.standardOutput buffers writes by default. Without explicit flushing, RPC notifications accumulate in the buffer (~4-8KB) and are delayed by 3-9 minutes in low-traffic scenarios.

As described in the issue:

  • Individual notification messages are small (~200-500 bytes)
  • Buffer doesn't fill quickly in low-traffic scenarios
  • Results in burst delivery when buffer finally flushes

Solution

Add try? FileHandle.standardOutput.synchronize() after each write in StdoutWriter.writeLine():

static func writeLine(_ line: String) {
  queue.sync {
    FileHandle.standardOutput.write(Data((line + "\n").utf8))
    // Flush immediately to ensure real-time delivery of RPC notifications
    try? FileHandle.standardOutput.synchronize()
  }
}

Testing

This fix ensures imsg rpc watch notifications are delivered immediately, matching the real-time behavior of the underlying FSEvents watcher.


🤖 Generated with Claude Code

Adds synchronize() call after stdout writes to prevent buffering delays.

Without explicit flushing, Swift's FileHandle.standardOutput buffers
writes until the buffer fills (~4-8KB). This causes RPC notifications
to be delayed by 3-9 minutes in low-traffic scenarios, as small JSON
messages (~200-500 bytes) accumulate in the buffer.

The fix ensures immediate delivery of watch notifications to clients.

Fixes steipete#23

Co-Authored-By: Claude <noreply@anthropic.com>
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.

RPC notifications delayed 3-9 minutes due to missing stdout flush

1 participant