Skip to content

Implement upload directory creation and lockfile handling#608

Open
MarcelGeo wants to merge 1 commit intodevelopfrom
lockfile-upload-creation
Open

Implement upload directory creation and lockfile handling#608
MarcelGeo wants to merge 1 commit intodevelopfrom
lockfile-upload-creation

Conversation

@MarcelGeo
Copy link
Copy Markdown
Collaborator

Move lockfile creation to Upload instance creation to prevent is_active collisions within multiple uploads.

This could be replaced by upcoming race conditions and lockfile removing.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts the v2 project push flow to create the upload transaction directory and lockfile earlier (before committing the Upload row) to reduce race conditions where concurrent workers may mis-detect an upload as inactive.

Changes:

  • Create upload_dir and lockfile before db.session.commit() when starting an upload transaction.
  • Ensure filesystem artifacts are moved to tmp when an IntegrityError occurs (including the retry path).
  • Add a local upload_dir variable to support cleanup after rollback/instance expiration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +303 to +308
# Save path as local before any potential rollback expires the ORM instance
upload_dir = upload.upload_dir
# Create dir and lockfile BEFORE committing so concurrent is_active() checks
# always see this upload as active once its DB row is visible to other workers.
os.makedirs(upload_dir)
open(upload.lockfile, "w").close()
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

os.makedirs(upload_dir) and creating the lockfile can raise OSError/PermissionError/FileExistsError, but this try only handles IntegrityError. If either filesystem call fails, the request will 500 without rolling back the DB session or cleaning up partially created artifacts. Consider catching OSError around the directory/lockfile creation, calling db.session.rollback(), and moving the directory to tmp (or removing it) before returning an UploadError response.

Copilot uses AI. Check for mistakes.
Comment on lines 332 to 336
db.session.add(upload)
upload_dir = upload.upload_dir
os.makedirs(upload_dir)
open(upload.lockfile, "w").close()
db.session.commit()
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

Same issue in the retry path: filesystem creation (os.makedirs / lockfile write) is not protected, but only IntegrityError is caught. A filesystem failure here will also leave the session in a bad state and return a 500. Handle OSError (rollback + cleanup) consistently in this block too.

Copilot uses AI. Check for mistakes.
Comment on lines +305 to +308
# Create dir and lockfile BEFORE committing so concurrent is_active() checks
# always see this upload as active once its DB row is visible to other workers.
os.makedirs(upload_dir)
open(upload.lockfile, "w").close()
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

PR description says lockfile creation was moved to Upload instance creation, but the lockfile/directory creation is still implemented in the controller. Either update the PR description or move the responsibility into Upload (or a helper) to keep the change aligned with the stated intent.

Copilot uses AI. Check for mistakes.
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.

2 participants