Skip to content

Copy shared assets when a symlink cannot be created - #7962

Open
DanShinde wants to merge 2 commits into
reflex-dev:mainfrom
DanShinde:fix-shared-asset-symlink-fallback
Open

DanShinde wants to merge 2 commits into
reflex-dev:mainfrom
DanShinde:fix-shared-asset-symlink-fallback

Conversation

@DanShinde

@DanShinde DanShinde commented Nov 22, 2026 •

Copy link
Copy Markdown

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Description

rx.asset(..., shared=True) links the asset into assets/external/ with a symlink. Where a symlink cannot be created (Windows without Developer Mode / the symlink privilege, or a filesystem without symlink support), symlink_to raises OSError and the app fails to compile, so any library using shared assets blocks reflex run.

_link_shared_asset now 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 on main and pass with the change; the whole file passes (32 tests). ruff and pyright are clean. Changelog fragment added under news/.

Closes #5997

View guided diff

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
@DanShinde
DanShinde requested a review from a team as a code owner November 22, 2026 07:15
@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 3/5

[Medium impact] The changes affect shared-asset compilation and cleanup.

Fix the source-record races before merging; parallel builds can still fail.

Findings

  1. P1 Concurrent record writes fail ▶
  2. P1 Cleanup reads vanished records ▶

Summary

The PR copies shared assets when symlinks are unavailable and tracks their sources for cleanup.

  • Shared assets are copied when a symlink cannot be created.
  • Startup cleanup removes copied assets whose source files are gone.

Reviews (2) · Last reviewed commit: "Compare copies by content and clean up s..." · Reviewed by Greptile

Comment thread reflex/assets.py Resolved
Comment thread reflex/assets.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread reflex/assets.py Resolved
Comment thread reflex/assets.py Resolved
Comment thread reflex/assets.py Resolved
- 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.
Comment thread reflex/assets.py
tmp_record = record.with_name(f".{uuid.uuid4().hex}.tmp")
try:
tmp_record.write_text(source, encoding="utf-8")
tmp_record.replace(record)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Concurrent record writes fail

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.

Comment thread reflex/assets.py
Comment on lines +232 to +233
for record in [p for p in records_dir.rglob("*") if p.is_file()]:
if Path(record.read_text(encoding="utf-8")).exists():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread reflex/assets.py
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 link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Comment thread reflex/assets.py
tmp_record = record.with_name(f".{uuid.uuid4().hex}.tmp")
try:
tmp_record.write_text(source, encoding="utf-8")
tmp_record.replace(record)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
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))

Comment thread reflex/assets.py
Comment on lines +232 to +239
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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
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)

This branch has not been deployed

No deployments
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.

If creating a symlink fails, reflex cannot start

1 participant