api: close file descriptor set in _dispatch

Make sure file descriptors are never leaked by closing them after
the `_message` method invocation. Clients that want to hold on to
fds past the scope of the method should use `FdSet.steal` to
extract those.
Adapt the `LoopServer`'s `_message` implementation accordingly.
This commit is contained in:
Christian Kellner 2020-07-27 12:02:54 +02:00 committed by Tom Gundersen
parent 0203dc4ccd
commit 630f73ba01
2 changed files with 6 additions and 2 deletions

View file

@ -52,7 +52,11 @@ class BaseAPI(abc.ABC):
@abc.abstractmethod
def _message(self, msg: Dict, fds: jsoncomm.FdSet, sock: jsoncomm.Socket, addr: str):
"""Called for a new incoming message"""
"""Called for a new incoming message
The file descriptor set `fds` will be closed after the call.
Use the `FdSet.steal()` method to extract file descriptors.
"""
def _cleanup(self):
"""Called after the event loop is shut down"""
@ -66,6 +70,7 @@ class BaseAPI(abc.ABC):
"""Called when data is available on the socket"""
msg, fds, addr = sock.recv()
self._message(msg, fds, sock, addr)
fds.close()
def _run_event_loop(self):
with jsoncomm.Socket.new_server(self.socket_address) as server: