@@ -56,26 +56,33 @@ def __init__(self, socket_path=None, auto_reconnect=False, display=None):
56
56
57
57
if socket_path :
58
58
logger .info ('using user provided socket path: %s' , socket_path )
59
- else :
60
- socket_path = self ._find_socket_path (display )
61
-
62
- if not socket_path :
63
- raise Exception ('Failed to retrieve the i3 or sway IPC socket path' )
59
+ # else _find_socket_path() will be used
64
60
65
61
self .subscriptions = 0
66
62
self ._pubsub = PubSub (self )
63
+ self ._display = display
67
64
self ._socket_path = socket_path
65
+ self ._connection_socket_path = self ._get_socket_path ()
68
66
self ._cmd_socket = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
69
- self ._cmd_socket .connect (self ._socket_path )
67
+ self ._cmd_socket .connect (self ._connection_socket_path )
70
68
self ._cmd_lock = Lock ()
71
69
self ._sub_socket = None
72
70
self ._sub_lock = Lock ()
73
71
self ._auto_reconnect = auto_reconnect
74
72
self ._quitting = False
75
73
self ._synchronizer = None
76
- self ._display = display
77
74
78
- def _find_socket_path (self , disp = None ):
75
+ def _get_socket_path (self ):
76
+ """Returns a current socket path."""
77
+ if self ._socket_path :
78
+ socket_path = self ._socket_path
79
+ else :
80
+ socket_path = self ._find_socket_path ()
81
+ if not socket_path :
82
+ raise Exception ('Failed to retrieve the i3 or sway IPC socket path' )
83
+ return socket_path
84
+
85
+ def _find_socket_path (self ):
79
86
socket_path = os .environ .get ("I3SOCK" )
80
87
if socket_path :
81
88
logger .info ('got socket path from I3SOCK env variable: %s' , socket_path )
@@ -86,8 +93,8 @@ def _find_socket_path(self, disp=None):
86
93
logger .info ('got socket path from SWAYSOCK env variable: %s' , socket_path )
87
94
return socket_path
88
95
89
- if disp :
90
- env = {** os .environ , 'DISPLAY' : disp }
96
+ if self . _display :
97
+ env = {** os .environ , 'DISPLAY' : self . _display }
91
98
else :
92
99
env = None
93
100
for binary in ('i3' , 'sway' ):
@@ -121,7 +128,7 @@ def socket_path(self) -> str:
121
128
122
129
:rtype: str
123
130
"""
124
- return self ._socket_path
131
+ return self ._connection_socket_path or self . _socket_path
125
132
126
133
@property
127
134
def auto_reconnect (self ) -> bool :
@@ -182,14 +189,16 @@ def _ipc_send(self, sock, message_type, payload):
182
189
183
190
def _wait_for_socket (self ):
184
191
# for the auto_reconnect feature only
185
- socket_path_exists = False
186
192
for tries in range (0 , 500 ):
187
- socket_path_exists = os .path .exists (self ._socket_path )
188
- if socket_path_exists :
189
- break
193
+ try :
194
+ self ._connection_socket_path = self ._get_socket_path ()
195
+ if os .path .exists (self ._connection_socket_path ):
196
+ return True
197
+ except Exception :
198
+ pass
190
199
time .sleep (0.001 )
191
200
192
- return socket_path_exists
201
+ return False
193
202
194
203
def _message (self , message_type , payload ):
195
204
try :
@@ -198,15 +207,16 @@ def _message(self, message_type, payload):
198
207
except ConnectionError as e :
199
208
if not self .auto_reconnect :
200
209
raise e
210
+ self ._connection_socket_path = None
201
211
202
212
logger .info ('got a connection error, reconnecting' , exc_info = e )
203
- # XXX: can the socket path change between restarts?
213
+ # The socket path can change between restarts.
204
214
if not self ._wait_for_socket ():
205
215
logger .info ('could not reconnect' )
206
216
raise e
207
217
208
218
self ._cmd_socket = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
209
- self ._cmd_socket .connect (self ._socket_path )
219
+ self ._cmd_socket .connect (self ._connection_socket_path )
210
220
return self ._ipc_send (self ._cmd_socket , message_type , payload )
211
221
finally :
212
222
self ._cmd_lock .release ()
@@ -462,7 +472,7 @@ def _on(self, event: Union[Event, str], handler: Callable[['Connection', IpcBase
462
472
463
473
def _event_socket_setup (self ):
464
474
self ._sub_socket = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
465
- self ._sub_socket .connect (self ._socket_path )
475
+ self ._sub_socket .connect (self ._connection_socket_path )
466
476
467
477
self ._subscribe (self .subscriptions )
468
478
0 commit comments