shared-memory interface¶
The shared-memory interface allows two snaps to communicate with each other using a specific predefined shared-memory path or directory in /dev/shm, an area of a POSIX-compliant filesystem reserved for shared memory. The location is defined by one snap and connected to from another.
Requires snapd version 2.54+ .
Developer details¶
Permissions |
||
|---|---|---|
no by default |
yes when |
|
no for plugs |
yes for slots |
Attributes:
shared-memory(slot and plug): optional, arbitrary identifier for the shared memory area(s) defined in the slot. A consumer snap must use the same identifier as the provider snap in order to work on the same shared memory object(s). Defaults to either local slot name or local plug name for slot/plug definitions respectively.private(plug): whentrue, creates a directory that is only accessible to the snap. This directory has read/write permissions, is mounted over/dev/shm, and permits an auto-connection to thesystem:shared-memoryslot.read(slot): list of read-only paths (after the implicit/dev/shm/) to be exposed to a consuming snap.write(slot): list of read and write paths (after the implicit/dev/shm/) to be exposed to a consuming snap.
The read and write attributes are used on the slot side to specify the names of the shared memory objects being shared (in read-only mode for read, and with full read-write mode for write); the prefix /dev/shm/ is implicit and must not be specified. Both attributes can be specified simultaneously for different paths, but the two values should not duplicate each other.
Code examples¶
An example plug definition:
plugs:
my-ipc:
interface: shared-memory
# this could be omitted since we already adjusted the plug name
# to match the slot's shared-memory name:
shared-memory: my-ipc
A matching slot definition which would auto-connect if in a snap from the same publisher:
slots:
shmem:
interface: shared-memory
shared-memory: my-ipc
write: [ students ] # gives read/write access to /dev/shm/students
read: [ teachers ] # gives readonly access to /dev/shm/teachers
Creating private shared memory for the snap:
plugs:
shared-memory:
private: true
When private: true is used, the shared-memory interface is automatically connected:
$ snap connections <example-private-shared-memory-snap>
Interface Plug Slot Notes
shared-memory os-release:shared-memory :shared-memory -
The test code can be found in the snapd repository: shared_memory_test.go. The source code for the interface is in the snapd repository: shared_memory.go
Common issues¶
Semaphore errors in Python¶
When using the interface in non-private mode with multi-processing Python, one might encounter something like this:
ProcessPoolExecutor(max_workers=max_workers) as executor:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python/lib/python3.11/concurrent/futures/process.py", line 732, in __init__
self._call_queue = _SafeQueue(
^^^^^^^^^^^
File "/opt/python/lib/python3.11/concurrent/futures/process.py", line 173, in __init__
super().__init__(max_size, ctx=ctx)
File "/opt/python/lib/python3.11/multiprocessing/queues.py", line 43, in __init__
self._rlock = ctx.Lock()
^^^^^^^^^^
File "/opt/python/lib/python3.11/multiprocessing/context.py", line 68, in Lock
return Lock(ctx=self.get_context())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python/lib/python3.11/multiprocessing/synchronize.py", line 169, in __init__
SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)
File "/opt/python/lib/python3.11/multiprocessing/synchronize.py", line 57, in __init__
sl = self._semlock = _multiprocessing.SemLock(
^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied`
The error comes from Python semaphores attempting to access /dev/shm. To remedy, use private mode instead that bind-mounts a snap-private /dev/shm and then grants access to all of it.