Code Monkey home page Code Monkey logo

Comments (11)

nhooey avatar nhooey commented on June 1, 2024 1

Hey @dirk-attraktor, I've noticed your issue but haven't read into the detailed code yet.

Would you like to provide a pull request that I can merge and release? I'll test the code.

from tendo.

andrew-azarov avatar andrew-azarov commented on June 1, 2024

The real fix is to remove
fcntl.lockf(self.fp, fcntl.LOCK_UN)
Because lock will be removed when you delete the file

from tendo.

dirk-makerhafen avatar dirk-makerhafen commented on June 1, 2024

No thats not "the real fix". Did you even bother to run the test code?
I updated the test code to a nicer testing suite:

https://github.com/dirk-attraktor/Singleton-Test I know this "Delete removes lock" is stated all over the internet, but what it actually means is "delete removes the lock and then deletes the file". See the race condition? I did not find a way to delete the lock file that does not result in a race condition.

EDIT: what @andrew-azarov says.

Regards
Dirk

from tendo.

andrew-azarov avatar andrew-azarov commented on June 1, 2024

I know this "Delete removes lock" is stated all over the internet, but what it actually means is
"delete removes the lock and then deletes the file".

Not exactly. It is done atomically at the FS level. What it means - nothing can read or write or lock the file until the bulk transaction is finished.

In your way there is short short time between calls. So say run your program with cpulimit -l 1 for example (this will limit it to 1% of available CPU and it will be really slow) and if you unlock the file and then another process comes to terms to lock it within the timeframe between calls to unlock and to delete means your app will allow the second app to run.

The probability is low but it is there and if it happens - it would be horrible to debug because your code advertises as a locking code

The only correct one which works atomically in all modern operating systems is https://github.com/dirk-attraktor/Singleton-Test/blob/master/SingletonOsremove.py .

However to atomically lock after creation you need to os.open it with the flags to lock before and not just create the file and lock it (same race condition).

from tendo.

dirk-makerhafen avatar dirk-makerhafen commented on June 1, 2024

Could you elaborate the last sentence?
"you need to os.open it with the flags to lock before and not just create the file and lock it"
Can you show an example how to do that?
I tried to open the file with os.O_SHLOCK or os.O_EXLOCK, but both flags seem to be BSB specific and don't exist on linux. https://docs.python.org/2/library/os.html#open-constants

Regards
Dirk

from tendo.

dirk-makerhafen avatar dirk-makerhafen commented on June 1, 2024

Ok i did some more testing. You are right:

The correct way is

os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)

to acquire a lock, and

os.remove(self.lockfile)

to release it. No fcntl needed what so ever.

Regards
Dirk

from tendo.

nhooey avatar nhooey commented on June 1, 2024

from tendo.

dirk-makerhafen avatar dirk-makerhafen commented on June 1, 2024

This is not done yet :)

What should happen if the program crashes or the computer just suddenly resets?
__del__ might not be called, and the lock file won't be deleted, effectively stalling further code executions until the lock file is manually deleted.
Thats not a good solution.
This does not happen with fcntl, but then the lock file will be around forever.

from tendo.

andrew-azarov avatar andrew-azarov commented on June 1, 2024

@dirk-attraktor that's an easy one and solved long time before us for daemons (it's called .pid file).

Put the lockfile creator PID into the file.
When another script runs and reads the file but finds no process - it can replace the line with its own PID.

from tendo.

andrew-azarov avatar andrew-azarov commented on June 1, 2024

So I made the module based on your singleton. It passes all the unittests however they are not very wide. I'd really want you to test it out and include if you want it. Also I saw your software marks MIT-compatible license (BSD), however I cannot find any copyright/LICENSE in any files except your main tendo project where there are just licenses from PSF which hardly work here.

https://github.com/andrew-azarov/singletony

from tendo.

ssbarnea avatar ssbarnea commented on June 1, 2024

I don't use this module anymore but if someone makes PRs, I will review, test, merge and release new versions.

from tendo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.