3131 BusyError ,
3232 InvalidInputError ,
3333 MissingFeatureError ,
34+ NoViewerError ,
3435 Refusal ,
3536 ViewerClosedError ,
3637 ViewerRefusedError ,
@@ -116,7 +117,12 @@ def __init__(
116117 self ._shared : dict [str , Any ] = {"client" : None , "stop" : self ._stop }
117118 weakref .finalize (self , _release_shared , self ._shared )
118119 self ._thread : threading .Thread | None = None
120+ # _dead: the data connection failed. A later send opens a new connection.
121+ # _closed: close() was called. Only connect() opens the viewer again.
119122 self ._dead = False
123+ self ._closed = False
124+ self ._address_lock = threading .Lock ()
125+ self ._address : discovery .ViewerAddress | None = None
120126 self ._last_send = 0.0
121127 self ._seq = 0
122128 self ._tool_lock = threading .Lock ()
@@ -157,7 +163,7 @@ def connected(self) -> bool:
157163 def tool_connected (self ) -> bool :
158164 """True while the tool connection is open."""
159165 tool = self ._tool_connection
160- return tool is not None and tool .connected and not self ._dead
166+ return tool is not None and tool .connected and not self ._closed
161167
162168 # -- context manager ------------------------------------------------------
163169
@@ -204,17 +210,21 @@ def connect(self) -> Viewer:
204210
205211 if self .connected :
206212 return self
213+ self ._closed = False
207214 self ._stop_thread ()
208215 self ._drop_client ()
209- address = discovery .find_viewer (
210- port = self ._port ,
211- token = self ._token ,
212- host = self ._host ,
213- settings = self ._settings_dir ,
214- )
215- client = WebSocketClient .connect (
216- address .host , address .port , token = address .token , timeout = self ._timeout , path = "/"
217- )
216+ address = self ._resolve_address ()
217+ try :
218+ client = WebSocketClient .connect (
219+ address .host , address .port , token = address .token , timeout = self ._timeout , path = "/"
220+ )
221+ except NoViewerError :
222+ # The viewer at the known address is gone. Search again.
223+ self ._forget_address ()
224+ address = self ._resolve_address ()
225+ client = WebSocketClient .connect (
226+ address .host , address .port , token = address .token , timeout = self ._timeout , path = "/"
227+ )
218228 try :
219229 _read_early (client )
220230 client .send_binary (encode .hello_frame (self ._crs , self ._proposed_origin , __version__ ))
@@ -1168,7 +1178,8 @@ def release_data(self) -> None:
11681178 """Close the connection so another client can own the scene.
11691179
11701180 A later ``send`` opens a new connection. The revision numbers stay. The
1171- tool connection stays open.
1181+ tool connection stays open. A later ``send`` also opens a new connection
1182+ after the connection fails.
11721183 """
11731184 self ._stop_thread ()
11741185 self ._drop_client ()
@@ -1179,38 +1190,68 @@ def close(self) -> None:
11791190 for watcher in watchers :
11801191 watcher .stop ()
11811192 with self ._tool_lock :
1182- self ._dead = True
1193+ self ._closed = True
11831194 tool , self ._tool_connection = self ._tool_connection , None
11841195 self ._colour_map_names = None
11851196 if tool is not None :
11861197 tool .close ()
11871198 self ._stop_thread ()
11881199 self ._drop_client ()
11891200 self ._dead = True
1201+ with self ._address_lock :
1202+ self ._address = None
11901203
11911204 # -- helpers --------------------------------------------------------------
11921205
11931206 def _tool (self ) -> ToolConnection :
11941207 """Return the tool connection. Make it at the first call."""
1195- if self ._dead :
1208+ if self ._closed :
11961209 raise ViewerClosedError
11971210 with self ._tool_lock :
1198- if self ._dead :
1211+ if self ._closed :
11991212 raise ViewerClosedError
12001213 if self ._tool_connection is None :
12011214 self ._tool_connection = ToolConnection (
1215+ timeout = self ._timeout , resolve = self ._resolve_address
1216+ )
1217+ return self ._tool_connection
1218+
1219+ def _resolve_address (self ) -> discovery .ViewerAddress :
1220+ """Return the address of the viewer.
1221+
1222+ While the data connection or the tool connection is open, the known address
1223+ is used again, so that both connections go to the same viewer. Otherwise the
1224+ viewer is found again.
1225+ """
1226+ with self ._address_lock :
1227+ tool = self ._tool_connection
1228+ in_use = (self ._client is not None and not self ._dead ) or (
1229+ tool is not None and tool .connected
1230+ )
1231+ if self ._address is None or not in_use :
1232+ self ._address = discovery .find_viewer (
12021233 port = self ._port ,
12031234 token = self ._token ,
12041235 host = self ._host ,
1205- settings_dir = self ._settings_dir ,
1206- timeout = self ._timeout ,
1236+ settings = self ._settings_dir ,
12071237 )
1208- return self ._tool_connection
1238+ return self ._address
1239+
1240+ def _forget_address (self ) -> None :
1241+ """Forget the known address and close the tool connection that uses it."""
1242+ with self ._tool_lock :
1243+ tool = self ._tool_connection
1244+ self ._colour_map_names = None
1245+ if tool is not None :
1246+ tool .close ()
1247+ with self ._address_lock :
1248+ self ._address = None
12091249
12101250 def _ensure_connected (self ) -> None :
1211- if self ._dead :
1251+ if self ._closed :
12121252 raise ViewerClosedError
1213- if self ._client is None :
1253+ if self ._client is None or self ._dead :
1254+ # No connection yet, or the last one failed: open a new one.
12141255 self .connect ()
12151256
12161257 def _drop_client (self ) -> None :
0 commit comments