Skip to content

Join server threads before closing IO pipes in stdio client tests#280

Open
koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
koic:fix_stdio_test_thread_race
Open

Join server threads before closing IO pipes in stdio client tests#280
koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
koic:fix_stdio_test_thread_race

Conversation

@koic
Copy link
Copy Markdown
Member

@koic koic commented Mar 30, 2026

Motivation and Context

Background threads simulating server responses could race with the ensure block that closes IO pipes, intermittently producing this warning depending on thread scheduling:

#<Thread:0x00007fb69290cbd0 test/mcp/client/stdio_test.rb:28 run>
terminated with exception (report_on_exception is true):
test/mcp/client/stdio_test.rb:55:in `write': stream closed in another thread (IOError)

Store each background thread in a server_thread variable and call server_thread&.join(1) at the top of each ensure block to wait for the thread to finish before closing pipes.

How Has This Been Tested?

This verifies that no race condition occurs.

Breaking Changes

None. This change updates only the test code.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Background threads simulating server responses could race with the `ensure` block
that closes IO pipes, intermittently producing this warning depending on thread scheduling:

```console
#<Thread:0x00007fb69290cbd0 test/mcp/client/stdio_test.rb:28 run>
terminated with exception (report_on_exception is true):
test/mcp/client/stdio_test.rb:55:in `write': stream closed in another thread (IOError)
```

Store each background thread in a `server_thread` variable and call `server_thread&.join(1)`
at the top of each `ensure` block to wait for the thread to finish before closing pipes.
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