Skip to content

fix(shell): stop stdin_write from blocking the main thread - #3782

Open
AllahuRodrigues wants to merge 1 commit into
tauri-apps:v2from
AllahuRodrigues:fix/shell-stdin-write-async
Open

AllahuRodrigues wants to merge 1 commit into
tauri-apps:v2from
AllahuRodrigues:fix/shell-stdin-write-async

Conversation

@AllahuRodrigues

Copy link
Copy Markdown

Fixes #3748.

Hey! So I looked into the issue where calling stdin_write can freeze the application window when the child process isn't reading from stdin.

The problem seems to be that stdin_write is currently synchronous and calls write_all directly while holding the children lock.

So if the child process stops reading or takes a while to read, the write blocks once the pipe buffer fills up, and the whole window can freeze until the child starts reading again.

What I changed

Initially, I considered making the command async, similar to the kill change in #3351.

But there's a potential issue with that approach. If JavaScript calls child.write() multiple times without awaiting each one, the writes could execute on different threads and potentially arrive out of order.

So instead, I kept the command synchronous but changed what it actually does.

Now, rather than writing directly to stdin, it copies the data into a queue for that child process.

A background writer thread then takes care of writing everything to stdin in the correct order.

The children lock is only held briefly while adding data to the queue, so the actual write no longer blocks the window.

A couple of things worth mentioning

Since the actual write happens in the background, errors such as the child process exiting are reported on the next write() call.

Also, when the child is dropped, the queued data is drained before stdin closes.

Testing

I added two tests.

  1. write_queued_does_not_wait_for_reader_and_keeps_order

    This writes 1 MiB of data followed by a short string to:

    sh -c 'sleep 1; cat'
    

    The child waits one second before reading anything.

    With the new implementation, both writes return immediately, and the output matches the input byte for byte.

    I also ran this against the old implementation to make sure the test actually catches the original issue. It failed with:

    blocked for 1.02s
    

    So the regression test does catch the blocking behavior.

  2. write_queued_reports_error_after_child_exits

    This checks that writing to a child process that has already exited correctly reports an error.

Final checks

  • All 26 shell tests passed.
  • Clippy passed with -D warnings.
  • Tested on macOS.
  • Haven't tested on Windows yet.

The new tests are under cfg(not(windows)), consistent with the other spawn tests in the file.

One limitation is that the queue is currently unbounded, so an application writing large amounts of data to a child that never reads could increase memory usage.

I considered adding a bounded queue, but wanted to keep this fix focused on the original blocking issue first.

Also, the public Rust CommandChild::write API is unchanged and still blocking. This only changes the JavaScript command path.

Let me know what you think, especially if you'd prefer a bounded queue or a different approach to handling write errors!

stdin_write is a sync command and did a blocking write_all while
holding the children lock, so a child that wasn't reading stdin froze
the window. It now queues the data for a per-child writer thread; the
command stays sync so writes keep their order.
@github-actions

Copy link
Copy Markdown
Contributor

Package Changes Through ba5d898

There are 2 changes which include shell with patch, shell-js with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
api-example 2.0.58 2.0.59
api-example-js 2.0.54 2.0.55
shell 2.6.0 2.6.1
shell-js 2.6.0 2.6.1

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@FabianLars

Copy link
Copy Markdown
Member

Since the actual write happens in the background, errors such as the child process exiting are reported on the next write() call.

i wonder if it makes sense or is possible to combine your queue with an async command so that you keep the order but also the background thread queue, or maybe that still adds the risk of out of order messsages 🤔

@AllahuRodrigues

Copy link
Copy Markdown
Author

Yeah, I considered that too. My main concern is actually where the writes enter the queue.

The queue itself preserves order, but if we make stdin_write async, Tauri can schedule those commands as separate tasks. So two child.write() calls made back to back in JS might not reach the queue in that same order.

That's why I kept the command sync. It only copies the bytes into the queue and returns. The actual blocking I/O happens on the writer thread.

So we're still moving the expensive part off the main thread, without introducing another ordering problem.

I think we could combine both approaches, but we'd need to guarantee ordering before the async tasks start enqueueing.

Another option would be to keep the sync enqueue and give each write an ID, then report its result once the background thread finishes. That would let us associate errors with the correct write, rather than reporting them on the next call.

It does add a bit more complexity though, so I didn't want to introduce that unless we actually need it.

Would you prefer that approach, or do you have another way in mind to preserve the call order with async commands?

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.

[shell] stdin_write blocks the main thread while the child isn't reading stdin

2 participants