smalltalkpharoseaside

Seaside: CannotDeleteFileException: Could not delete the old version of fuel file


In pharo seaside application on home button click all the data will get saved in a fuel file with some class name as Test.fl. If I call the same home page at a time in two instances, both are calling this below code to save Test.fl file with latest updates,

FLSerializer
    serialize: self allObjects
    toFileNamed: self name , '.fl'**

and getting exception:

CannotDeleteFileException: Could not delete the old version of file ...\Pharo3.0\Test.fl

And I have used Mutex to allow another instance to get executed after the first instance,

mutexBlock := Mutex new.
mutexBlock critical: [
    FLSerializer
        serialize: self allObjects
        toFileNamed: self name , '.fl' ]

But still i am getting the same error

CannotDeleteFileException: Could not delete the old version of file

Please anybody help me on this to fix the error.


Solution

  • Your intuition is probably correct: two processes are trying to write (delete) the same file at the same time. Your Mutex approach will not work however because you create a new Mutex every time you execute that code. You have to store the Mutex in a class or instance variable (depending on how your code works) such that every process will use the same instance of the Mutex.

    Your code would then look something like this:

    MyClass class>>serializeProtect
        ^ SerializeProtect ifNil: [ SerializeProtect := Mutex new ]
    
    self class serializeProtect critical: [
        FLSerializer
            serialize: self allObjects
            toFileNamed: self name , '.fl' ]
    

    Note: it is generally not safe to initialise a Mutex lazily. I'm simply doing it here for simplicities sake.