@@ -1530,6 +1530,49 @@ def test_write_failure_rolls_back_acquire(lock_path: Path, mocker: MockerFixture
15301530 assert not lock_path .exists ()
15311531
15321532
1533+ @pytest .mark .skipif (sys .platform == "win32" , reason = "POSIX rolls back before closing the descriptor" )
1534+ @NEEDS_STALE_RECLAIM
1535+ @pytest .mark .usefixtures ("unchanged_past_grace" )
1536+ @pytest .mark .parametrize (
1537+ ("cleanup_errno" , "fail_unlink" , "record" ),
1538+ [
1539+ pytest .param (ENOSPC , False , b"\0 " , id = "create-full" ),
1540+ pytest .param (ENOSPC , True , b"\0 " , id = "unlink-full" ),
1541+ pytest .param (EIO , False , b"" , id = "io-error" ),
1542+ ],
1543+ )
1544+ def test_rollback_on_full_filesystem (
1545+ lock_path : Path , mocker : MockerFixture , cleanup_errno : int , record : bytes , * , fail_unlink : bool
1546+ ) -> None : # pragma: win32 no cover
1547+ open_file = os .open
1548+
1549+ def open_with_full_directory (path : str , flags : int , mode : int = 0o777 ) -> int :
1550+ if not fail_unlink and ".break." in path :
1551+ raise OSError (cleanup_errno , os .strerror (cleanup_errno ))
1552+ return open_file (path , flags , mode )
1553+
1554+ unlink = Path .unlink
1555+
1556+ def unlink_on_full_filesystem (path : Path , * , missing_ok : bool = False ) -> None :
1557+ if fail_unlink and path == lock_path :
1558+ raise OSError (ENOSPC , os .strerror (ENOSPC ))
1559+ unlink (path , missing_ok = missing_ok )
1560+
1561+ unlinks = mocker .patch .object (Path , "unlink" , side_effect = unlink_on_full_filesystem , autospec = True )
1562+ opens = mocker .patch ("filelock._util.os.open" , side_effect = open_with_full_directory , autospec = True )
1563+ writes = mocker .patch ("filelock._util.os.write" , side_effect = OSError (ENOSPC , "record write failed" ), autospec = True )
1564+ with pytest .raises (OSError , match = "record write failed" ):
1565+ SoftFileLock (lock_path ).acquire (timeout = 0 )
1566+ mocker .stop (opens )
1567+ mocker .stop (writes )
1568+ mocker .stop (unlinks )
1569+ assert lock_path .read_bytes () == record
1570+ lock = SoftFileLock (lock_path )
1571+ acquire_after_reclaim (lock )
1572+ assert lock .is_locked == (cleanup_errno == ENOSPC )
1573+ lock .release ()
1574+
1575+
15331576def _hold_reports_foreign_identity (mocker : MockerFixture ) -> None :
15341577 # Make the held descriptor report a different inode than the file on disk, as if a peer replaced the marker at the
15351578 # path. fstat runs only on our own lock fd, so nothing else in acquire or release is disturbed.
0 commit comments