Repository navigation
Conversation
rx.asset(shared=True) links the asset into assets/external/ with a symlink. On Windows without the symlink privilege, or on a filesystem without symlink support, creating it raises OSError and the app cannot compile. Fall back to copying the asset through the same staged replace, and leave an up to date copy alone so file watchers see no change. Fixes reflex-dev#5997
[Medium impact] The changes affect shared-asset compilation and cleanup. Fix the source-record races before merging; parallel builds can still fail. FindingsSummaryThe PR copies shared assets when symlinks are unavailable and tracks their sources for cleanup.
Reviews (2) · Last reviewed commit: "Compare copies by content and clean up s..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
- Compare a fallback copy with its source byte by byte instead of with filecmp, whose cache misses a rewrite that keeps size and mtime. - When a replace is denied, accept the asset in place as either a link or a copy, so a concurrent compile that had to copy does not fail one that can link. - Record the source of each fallback copy under the web directory and remove copies whose source is gone, as broken symlinks already are.
| tmp_record = record.with_name(f".{uuid.uuid4().hex}.tmp") | ||
| try: | ||
| tmp_record.write_text(source, encoding="utf-8") | ||
| tmp_record.replace(record) |
There was a problem hiding this comment.
On Windows, two builds can reach _record_external_copy before either record exists. Unlike the asset replace, tmp_record.replace(record) has no retry or check for an identical record written by the other build. A denied replace then aborts the import, even though the correct asset is already in place.
Apply the same bounded retry and identical-content check to record writes.
| for record in [p for p in records_dir.rglob("*") if p.is_file()]: | ||
| if Path(record.read_text(encoding="utf-8")).exists(): |
There was a problem hiding this comment.
Cleanup reads vanished records
_remove_stale_external_copies includes temporary record files in its saved list. If another build finishes _record_external_copy after that list is collected, the temporary file is renamed away before record.read_text() runs. The uncaught FileNotFoundError then stops compilation.
Exclude staging files from cleanup and tolerate records disappearing while cleanup runs.
There was a problem hiding this comment.
3 issues found across 2 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="reflex/assets.py">
<violation number="1" location="reflex/assets.py:218">
P2: Retry `tmp_record.replace(record)` on `PermissionError` and accept an existing record with the same source; otherwise concurrent Windows compiles can abort after the asset copy succeeds.</violation>
<violation number="2" location="reflex/assets.py:232">
P2: Exclude `.tmp` staging records and handle `FileNotFoundError` while reading records; a concurrent replace can remove a listed path before `read_text` and abort compilation.</violation>
<violation number="3" location="reflex/assets.py:237">
P2: Stale-record cleanup can delete a replacement copy created by a concurrent compile. Synchronize cleanup with copy placement, or revalidate the record and destination under an interprocess lock before deleting either.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| continue | ||
| copy = external_dir / record.relative_to(records_dir) | ||
| # A symlink there was linked since, and is the symlink cleanup's to judge. | ||
| if copy.is_file() and not copy.is_symlink(): |
There was a problem hiding this comment.
P2: Stale-record cleanup can delete a replacement copy created by a concurrent compile. Synchronize cleanup with copy placement, or revalidate the record and destination under an interprocess lock before deleting either.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At reflex/assets.py, line 237:
<comment>Stale-record cleanup can delete a replacement copy created by a concurrent compile. Synchronize cleanup with copy placement, or revalidate the record and destination under an interprocess lock before deleting either.</comment>
<file context>
@@ -177,20 +179,81 @@ def _versioned_asset_path(relative_path: str, source_file: Path) -> AssetPathStr
+ continue
+ copy = external_dir / record.relative_to(records_dir)
+ # A symlink there was linked since, and is the symlink cleanup's to judge.
+ if copy.is_file() and not copy.is_symlink():
+ copy.unlink()
+ record.unlink()
</file context>
| tmp_record = record.with_name(f".{uuid.uuid4().hex}.tmp") | ||
| try: | ||
| tmp_record.write_text(source, encoding="utf-8") | ||
| tmp_record.replace(record) |
There was a problem hiding this comment.
P2: Retry tmp_record.replace(record) on PermissionError and accept an existing record with the same source; otherwise concurrent Windows compiles can abort after the asset copy succeeds.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At reflex/assets.py, line 218:
<comment>Retry `tmp_record.replace(record)` on `PermissionError` and accept an existing record with the same source; otherwise concurrent Windows compiles can abort after the asset copy succeeds.</comment>
<file context>
@@ -177,20 +179,81 @@ def _versioned_asset_path(relative_path: str, source_file: Path) -> AssetPathStr
+ tmp_record = record.with_name(f".{uuid.uuid4().hex}.tmp")
+ try:
+ tmp_record.write_text(source, encoding="utf-8")
+ tmp_record.replace(record)
+ finally:
+ tmp_record.unlink(missing_ok=True)
</file context>
| tmp_record.replace(record) | |
| for attempt in range(_MAX_LINK_ATTEMPTS): | |
| try: | |
| tmp_record.replace(record) | |
| break | |
| except PermissionError: | |
| try: | |
| if record.read_text(encoding="utf-8") == source: | |
| break | |
| except OSError: | |
| pass | |
| if attempt == _MAX_LINK_ATTEMPTS - 1: | |
| raise | |
| time.sleep(_LINK_RETRY_DELAY * (attempt + 1)) |
| for record in [p for p in records_dir.rglob("*") if p.is_file()]: | ||
| if Path(record.read_text(encoding="utf-8")).exists(): | ||
| continue | ||
| copy = external_dir / record.relative_to(records_dir) | ||
| # A symlink there was linked since, and is the symlink cleanup's to judge. | ||
| if copy.is_file() and not copy.is_symlink(): | ||
| copy.unlink() | ||
| record.unlink() |
There was a problem hiding this comment.
P2: Exclude .tmp staging records and handle FileNotFoundError while reading records; a concurrent replace can remove a listed path before read_text and abort compilation.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At reflex/assets.py, line 232:
<comment>Exclude `.tmp` staging records and handle `FileNotFoundError` while reading records; a concurrent replace can remove a listed path before `read_text` and abort compilation.</comment>
<file context>
@@ -177,20 +179,81 @@ def _versioned_asset_path(relative_path: str, source_file: Path) -> AssetPathStr
+ records_dir = _external_copy_records_dir()
+ if not records_dir.is_dir():
+ return
+ for record in [p for p in records_dir.rglob("*") if p.is_file()]:
+ if Path(record.read_text(encoding="utf-8")).exists():
+ continue
</file context>
| for record in [p for p in records_dir.rglob("*") if p.is_file()]: | |
| if Path(record.read_text(encoding="utf-8")).exists(): | |
| continue | |
| copy = external_dir / record.relative_to(records_dir) | |
| # A symlink there was linked since, and is the symlink cleanup's to judge. | |
| if copy.is_file() and not copy.is_symlink(): | |
| copy.unlink() | |
| record.unlink() | |
| for record in records_dir.rglob("*"): | |
| if not record.is_file() or ( | |
| record.name.startswith(".") and record.name.endswith(".tmp") | |
| ): | |
| continue | |
| try: | |
| source_exists = Path(record.read_text(encoding="utf-8")).exists() | |
| except FileNotFoundError: | |
| continue | |
| if source_exists: | |
| continue | |
| copy = external_dir / record.relative_to(records_dir) | |
| # A symlink there was linked since, and is the symlink cleanup's to judge. | |
| if copy.is_file() and not copy.is_symlink(): | |
| copy.unlink(missing_ok=True) | |
| record.unlink(missing_ok=True) |
All Submissions:
Type of change
Changes To Core Features:
Description
rx.asset(..., shared=True)links the asset intoassets/external/with a symlink. Where a symlink cannot be created (Windows without Developer Mode / the symlink privilege, or a filesystem without symlink support),symlink_toraisesOSErrorand the app fails to compile, so any library using shared assets blocksreflex run._link_shared_assetnow falls back to copying the asset when the symlink cannot be created. The copy goes through the same staged temp file and atomic replace as the link, so concurrent compiles stay safe, and the Windows replace-denied concession accepts an identical copy just as it accepts the same link. An existing copy with identical contents is left untouched, so file watchers see no change on recompile; a copy is rewritten when the source changes.Tests: three new unit tests in
tests/units/assets/test_assets.py(copy when symlinks are denied, up to date copy left alone, stale copy refreshed). They fail onmainand pass with the change; the whole file passes (32 tests). ruff and pyright are clean. Changelog fragment added undernews/.Closes #5997