Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "with" statement can occur TOCTOU?

Tags:

python

Assume that, file.txt is closed or deleted during the delay between open and write. (or it can ?)

Then, this situation can occur TOCTOU ?

with statement sure that atomic until with block or not?

with open("file.txt") as f :
    # ...delayed...
    f.write("something")
like image 632
Ho0ony Avatar asked Mar 28 '26 23:03

Ho0ony


1 Answers

Can this occur?

Use case 1: Python itself deletes the file*

yes it can happen. I just tested like this:

In [1]: with open("file.txt", "w") as f :
   ...:     f.write("Something Old")
   ...:

In [2]: !cat ./file.txt
Something Old
In [3]: import os
   ...: with open("file.txt","w") as f:
   ...:     os.remove("./file.txt")
   ...:     print f.write("Something new")
   ...:
None

In [4]: !cat ./file.txt
cat: ./file.txt: No such file or directory

Use Case 2: Other than python deletes the file.

Then also, found the behavior to be same.

In [1]: !cat ./file.txt
Something Old
In [2]: import os
   ...: import time
   ...:
   ...: with open("file.txt","w") as f:
   ...:     time.sleep(15)
   ...:     print f.write("Something new")
   ...:
None

In [3]: !cat ./file.txt
cat: ./file.txt: No such file or directory

How to avoid it?

You can use exclusive lock from fcntl.lockf()

Edit: There is one more caveat here. Locking the file may not be straight forward and may be OS dependent like What is the best way to open a file for exclusive access in Python?

like image 68
Gurupad Hegde Avatar answered Mar 30 '26 13:03

Gurupad Hegde