Skip to content

Commit 8bd1fff

Browse files
committed
Fix a bug to correctly cleanup after sending "Stop App" message. This allows a server to correctly handle the next client
1 parent dd8c23f commit 8bd1fff

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

simulaqron/netqasm_backend/qnodeos.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
from typing import Optional, Dict, Callable, Generator, Any, List, Type
44

55
from netqasm.backend.executor import Executor
6-
from netqasm.backend.messages import MsgDoneMessage, Message, MessageType
6+
from netqasm.backend.messages import MsgDoneMessage, Message, MessageType, StopAppMessage
77
from netqasm.backend.qnodeos import QNodeController
88
from netqasm.lang.instr import Flavour
9+
from netqasm.sdk.shared_memory import SharedMemoryManager
910
from twisted.internet.defer import inlineCallbacks
1011
from twisted.internet.protocol import Protocol
1112

@@ -173,6 +174,11 @@ def _return_qubit_state(self, qubit_id: int, real_part: List[List[float]], imag_
173174
qubit_state_message = ReturnQubitStateMessage(qubit_id, real_part, imag_part)
174175
self._return_msg(msg=qubit_state_message)
175176

177+
def _handle_stop_app(self, msg: StopAppMessage) -> Generator[Any, None, None]:
178+
yield from super()._handle_stop_app(msg)
179+
# Clear the shared memory registries occupied in the QNodeOS backend
180+
SharedMemoryManager.reset_memories()
181+
176182
# We override the _get_message_handlers method so we can also handle the "get qubit state" message
177183
def _get_message_handlers(self) -> Dict[NewMessageType | MessageType, Callable]:
178184
return {

simulaqron/sdk/connection.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,28 @@ def try_connection(
152152
retry_time=-1.0,
153153
)
154154

155+
def close(
156+
self,
157+
clear_app: bool = True,
158+
stop_backend: bool = False,
159+
exception: bool = False,
160+
) -> None:
161+
"""
162+
Closes the SimulaQron connection. It also sends the corresponding messages
163+
to the backend to clean up the states associated with the connection.
164+
This leaves the quantum backend ready to be used with a new client.
165+
166+
:param clear_app: Clear the application before closing the connection.
167+
:type clear_app: bool
168+
:param stop_backend: Stop the backend when closing the connection.
169+
:type stop_backend: bool
170+
:param exception: Whether the app is stopping due to an exception or not.
171+
:type exception: bool
172+
"""
173+
super().close(clear_app, stop_backend, exception)
174+
# Clear the shared memories occupied by this connection
175+
SharedMemoryManager.reset_memories()
176+
155177
@staticmethod
156178
def _create_socket(
157179
name: str,

0 commit comments

Comments
 (0)