feat: Add waiting logic for FIFO files that may not be immediately readable #599
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.
Fixes #598
python-dotenvalready had basic FIFO support (added in #586), it didn't work with asynchronously mounted FIFO files, like by 1Password's new (beta) Environment featExisting implementation opened FIFOs directly:
The flaw with this approach: when a FIFO exists but isn't immediately ready to read (common with async-mounted filesystems),
open()can block indefinitely waiting for a writer, or read it as empty.The existing test (
test_load_dotenv_from_fifo) worked because it started a writer thread before attempting to read, ensuring the FIFO was ready and had contentSolution
I've added a new
_wait_for_file_content()function that handles FIFOs that may not be immediately readable:stat.S_ISFIFO()to distinguish them from regular filesselect.select()to wait for the FIFO to become readable (with a 5-second timeout)rb,buffering=0) for accurate readiness detectionStringIOobjectThe result is that even when using regular FIFO or using 1Password, it reads (or prompts 1Password for an unlock password) correctly
Changes
_wait_for_file_content()function insrc/dotenv/main.py_get_stream()to use the new function for FIFO-aware file readingTesting
Impact
This change is backward compatible and imprpves support for: