How to clean up segments¶
A box lives in shared memory, and that memory outlives the Python object you use it through. So cleaning up takes two steps: each process closes the boxes it opened, and once no process needs a box any more, you remove the name of its segment. This guide also shows how to recover when a process crashed before cleaning up.
1. Close each box you open¶
Leave a with block, or call close yourself:
from sharedbox import LockTimeoutError, SharedBox, segment_status
class Delivery(SharedBox, name="example-delivery"):
done: bool = False
delivery = Delivery()
delivery.events.done.connect(lambda new, old: print(delivery.name, "done:", new))
delivery.done = True
delivery.close()
close lets go of this process's handle and stops its
watcher thread, while the data stays for the other
processes.
A callback that uses the box keeps it open
A callback that refers to the box, as the lambda above does, keeps the
box alive, so garbage collection never closes it for you. Call close
yourself on such a box.
2. Remove the name¶
unlink removes the name, so no process can
attach the box from then on:
Call it once, from the process that created the box;
Closing and lifetime
says why. On Linux the segment stays until it is unlinked. On Windows
unlink does nothing, because the segment goes away by itself when its last
handle is closed. Either way you write the same code, and it works on both.
Remove a segment left by a crash¶
On Linux, a process that crashed before calling unlink leaves its segment
behind as a file in /dev/shm. For one name,
segment_status says whether a box or stream
is published under it and whether the process that created it still runs:
It returns missing, running or creator_exited. A segment whose creator
has exited is one to remove once no other process uses it. A process that
crashed in the middle of create leaves a segment that was never published,
which reads as missing although the name is still taken; creating under
that name raises SegmentExistsError, which says so. To see every segment at
once, list the files:
If you try to create a box under a name that is left over, you get
SegmentExistsError, and its message says
whether the process that created it is still running. If it isn't, remove
the segment with unlink and the part of the file name after SBX:,
for example Delivery.unlink("delivery-1"), or simply delete the file.
On Windows nothing is left behind: a segment goes away with the last process that has it open.
Release a lock left by a crash¶
Only one process can write to a box at a time, so a writer holds the box's
write lock while it writes. A process that dies in the middle of a write
leaves that lock taken. From then on, every read and write waits for the class's
lock_timeout and then raises
LockTimeoutError, whose message names the
process that holds the lock. Once you know that process is gone, call
force_unlock on any box attached to
the same segment, then write again:
delivery = Delivery.attach()
try:
delivery.done = False
except LockTimeoutError as error:
print(error) # ... locked by pid 1234 ...
# only once process 1234 is no longer running
delivery.force_unlock()
print(delivery.done) # True: the write that timed out did not happen
delivery.done = False
print(delivery.done) # False
The write that timed out didn't happen, so the field keeps its old value until you write it again.
Only unlock a lock whose owner is gone
force_unlock doesn't check that the process holding the lock has
stopped. If it is still running, its half-finished write becomes
visible to readers. Check the process id (pid) in the error message
first, with ps -p <pid> on Linux or tasklist /FI "PID eq <pid>" on
Windows; force_unlock has the
details.
The whole script
"""The script of the guide "How to clean up segments"."""
from sharedbox import LockTimeoutError, SharedBox, segment_status
class Delivery(SharedBox, name="example-delivery"):
done: bool = False
delivery = Delivery()
delivery.events.done.connect(lambda new, old: print(delivery.name, "done:", new))
delivery.done = True
delivery.close()
Delivery.unlink()
print(segment_status("example-delivery")) # missing
creator = Delivery(True)
delivery = Delivery.attach()
try:
delivery.done = False
except LockTimeoutError as error:
print(error) # ... locked by pid 1234 ...
# only once process 1234 is no longer running
delivery.force_unlock()
print(delivery.done) # True: the write that timed out did not happen
delivery.done = False
print(delivery.done) # False
delivery.close()
creator.close()
Delivery.unlink()