-
Notifications
You must be signed in to change notification settings - Fork 242
[2.7] Fix FilePipe TOCTOU #4296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+215
−7
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
972e400
Fix FilePipe TOCTOU
YuanTingHsieh 0f972b6
Address review comments
YuanTingHsieh 17d8c4c
fix format
YuanTingHsieh 442f2cf
address comments
YuanTingHsieh fe36516
Merge branch '2.7' into fix_file_pipe_toctou
YuanTingHsieh 9ad50fe
Merge branch '2.7' into fix_file_pipe_toctou
YuanTingHsieh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import time | ||
|
|
||
| from nvflare.fuel.utils.constants import Mode | ||
| from nvflare.fuel.utils.pipe.pipe import Pipe, Topic | ||
| from nvflare.fuel.utils.pipe.pipe_handler import PipeHandler | ||
|
|
||
|
|
||
| class _BrokenPipe(Pipe): | ||
| """Minimal Pipe stub whose receive() always raises BrokenPipeError.""" | ||
|
|
||
| def __init__(self, error_msg): | ||
| super().__init__(mode=Mode.ACTIVE) | ||
| self._error_msg = error_msg | ||
|
|
||
| def open(self, name): | ||
| pass | ||
|
|
||
| def close(self): | ||
| pass | ||
|
|
||
| def send(self, msg, timeout=None): | ||
| return True | ||
|
|
||
| def receive(self, timeout=None): | ||
| raise BrokenPipeError(self._error_msg) | ||
|
|
||
| def get_last_peer_active_time(self): | ||
| return 0.0 | ||
|
|
||
| def clear(self): | ||
| pass | ||
|
|
||
| def can_resend(self) -> bool: | ||
| return False | ||
|
|
||
|
|
||
| class TestPipeHandlerBrokenPipe: | ||
| """PipeHandler._try_read must emit PEER_GONE and stop when receive() raises BrokenPipeError.""" | ||
|
|
||
| def _make_handler(self, pipe): | ||
| return PipeHandler( | ||
| pipe=pipe, | ||
| read_interval=0.01, | ||
| heartbeat_interval=5.0, | ||
| heartbeat_timeout=30.0, | ||
| ) | ||
|
|
||
| def _drain_messages(self, handler, timeout=1.0): | ||
| messages = [] | ||
| deadline = time.time() + timeout | ||
| while time.time() < deadline: | ||
| msg = handler.get_next() | ||
| if msg: | ||
| messages.append(msg) | ||
| time.sleep(0.01) | ||
| return messages | ||
|
|
||
| def test_pipe_closed_emits_peer_gone_and_stops(self): | ||
| """When receive() raises BrokenPipeError('pipe is not open'), PEER_GONE is emitted and the reader stops.""" | ||
| handler = self._make_handler(_BrokenPipe("pipe is not open")) | ||
| handler.start() | ||
|
|
||
| messages = self._drain_messages(handler, timeout=1.0) | ||
| handler.stop() | ||
|
|
||
| assert any(m.topic == Topic.PEER_GONE for m in messages) | ||
| assert handler.reader is None | ||
|
|
||
| def test_pipe_dir_gone_emits_peer_gone_and_stops(self): | ||
| """When receive() raises BrokenPipeError('error reading from ...'), PEER_GONE is emitted and the reader stops.""" | ||
| handler = self._make_handler(_BrokenPipe("error reading from /some/dir")) | ||
| handler.start() | ||
|
|
||
| messages = self._drain_messages(handler, timeout=1.0) | ||
| handler.stop() | ||
|
|
||
| assert any(m.topic == Topic.PEER_GONE for m in messages) | ||
| assert handler.reader is None | ||
|
|
||
| def test_graceful_stop_does_not_emit_peer_gone(self): | ||
| """BrokenPipeError raised after stop() is called must not emit PEER_GONE.""" | ||
| pipe = _BrokenPipe("pipe is not open") | ||
| handler = self._make_handler(pipe) | ||
|
|
||
| received = [] | ||
| handler.set_status_cb(lambda msg: received.append(msg)) | ||
|
|
||
| handler.start() | ||
| # Stop immediately — asked_to_stop=True before _try_read can emit anything. | ||
| handler.stop() | ||
|
|
||
| # Give the reader thread time to finish. | ||
| deadline = time.time() + 1.0 | ||
| while handler.reader is not None and time.time() < deadline: | ||
| time.sleep(0.01) | ||
|
|
||
| assert not any(m.topic == Topic.PEER_GONE for m in received) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.