Add File Locking Support to BLI_fileops #103935

Open
opened 2023-01-17 12:03:41 +01:00 by Bastien Montagne · 5 comments

UPDATE:

After talking with @brecht, it seems that writing in temp file + atomically moving it to the actual draft.blend file would match the requirements for the draft project:

  • On UNIX, this works 'out of the box', processes having 'old' draft.blend open will keep using it, new openings will have the new version of the file.
  • On Windows, the atomic move of the temp file to its final name will fail if the current draft.blend is used by other processes. This is acceptable and can be worked around (e.g. by looping a reasonable amount of times over the move call).
    The only change from the Draft handling PoV is that it needs to have a way to report failures to write to the user, in the rare case this should still happen. Not a bad thing to have anyway.

ORIGINAL Proposal:

The Draft project will require several instances of Blender being able to safely access a same draft .blend file, either for reading or writing.

This could be achieved in two ways:

  • Implement our own locking mechanism, e.g. using an additional draft.blend.lock with some extra info in it if we need R/W locks.
  • Use systems solutions, flock on UNIX, and LockFileEx on Windows.

Either solution would be implemented as new calls in BLI_fileops, e.g.

bool BLI_flock_shared(cons int file_descriptor, const bool is_blocking), bool BLI_flock_exclusive(cons int file_descriptor, const bool is_blocking), bool BLI_flock_release(cons int file_descriptor).

These will work on file descriptors as returned by e.g. BLI_open().

For the records, here is a partial implementation of using system's file locking feature (Windows code is not working, Linux and OSX implementation seems fine): D17022.

UPDATE: After talking with @brecht, it seems that writing in temp file + atomically moving it to the actual `draft.blend` file would match the requirements for the draft project: * On UNIX, this works 'out of the box', processes having 'old' draft.blend open will keep using it, new openings will have the new version of the file. * On Windows, the atomic move of the temp file to its final name will fail if the current `draft.blend` is used by other processes. This is acceptable and can be worked around (e.g. by looping a reasonable amount of times over the move call). The only change from the Draft handling PoV is that it needs to have a way to report failures to write to the user, in the rare case this should still happen. Not a bad thing to have anyway. -------------------------- ORIGINAL Proposal: The Draft project will require several instances of Blender being able to safely access a same draft .blend file, either for reading or writing. This could be achieved in two ways: - Implement our own locking mechanism, e.g. using an additional `draft.blend.lock` with some extra info in it if we need R/W locks. - Use systems solutions, `flock` on UNIX, and `LockFileEx` on Windows. Either solution would be implemented as new calls in `BLI_fileops`, e.g. `bool BLI_flock_shared(cons int file_descriptor, const bool is_blocking)`, `bool BLI_flock_exclusive(cons int file_descriptor, const bool is_blocking)`, `bool BLI_flock_release(cons int file_descriptor)`. These will work on file descriptors as returned by e.g. `BLI_open()`. For the records, here is a partial implementation of using system's file locking feature (Windows code is not working, Linux and OSX implementation seems fine): [D17022](https://archive.blender.org/developer/D17022).
Author
Owner

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Author
Owner

Added subscribers: @mont29, @dfelinto, @JulianEisel, @brecht

Added subscribers: @mont29, @dfelinto, @JulianEisel, @brecht

I'm not sure we should be using file locking. System specific solutions only work in some cases, not for network drives for example, so at a minimum we'd need our own solution. But then you need to deal with cases like a lock file that hangs around after a crash.

Personally I would save every brush in its own .blend file in a draft/ folder rather than a draft.blend, and maybe check file modified dates to prevent conflicts.

I'm not sure we should be using file locking. System specific solutions only work in some cases, not for network drives for example, so at a minimum we'd need our own solution. But then you need to deal with cases like a lock file that hangs around after a crash. Personally I would save every brush in its own .blend file in a draft/ folder rather than a draft.blend, and maybe check file modified dates to prevent conflicts.

So there was some confusion here. I assumed that the purpose of file locking was to somehow show the asset as locked in the Blender UI, to prevent a user from editing it in the multiple Blender instances. However this is not planned, and it seems acceptable to not prevent such conflicts.

The purpose here was to atomically overwrite draft.blend while another Blender application has it open for reading, which can instead by done by moving another file over the existing file. The one caveat is that on Windows this may fail when another application has the file open, which could be solved by retries of move command for the rare cases this happens.

So there was some confusion here. I assumed that the purpose of file locking was to somehow show the asset as locked in the Blender UI, to prevent a user from editing it in the multiple Blender instances. However this is not planned, and it seems acceptable to not prevent such conflicts. The purpose here was to atomically overwrite `draft.blend` while another Blender application has it open for reading, which can instead by done by moving another file over the existing file. The one caveat is that on Windows this may fail when another application has the file open, which could be solved by retries of move command for the rare cases this happens.
Author
Owner

@brecht thanks, updated the task, will keep it open as part of the Draft project for the time being.

@brecht thanks, updated the task, will keep it open as part of the Draft project for the time being.
Philipp Oeser removed the
Interest
Core
label 2023-02-09 14:42:45 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#103935
No description provided.