@@ -132,6 +132,9 @@ class PostgresNode(object):
132
132
# a max number of node start attempts
133
133
_C_MAX_START_ATEMPTS = 5
134
134
135
+ # a max number of read pid file attempts
136
+ _C_MAX_GET_PID_ATEMPTS = 5
137
+
135
138
def __init__ (self , name = None , base_dir = None , port = None , conn_params : ConnectionParams = ConnectionParams (),
136
139
bin_dir = None , prefix = None ):
137
140
"""
@@ -208,14 +211,40 @@ def pid(self):
208
211
Return postmaster's PID if node is running, else 0.
209
212
"""
210
213
211
- if self .status ():
212
- pid_file = os .path .join (self .data_dir , PG_PID_FILE )
213
- lines = self .os_ops .readlines (pid_file )
214
- pid = int (lines [0 ]) if lines else None
215
- return pid
214
+ nAttempt = 0
215
+ pid_file = os .path .join (self .data_dir , PG_PID_FILE )
216
+ pid_s : str = None
217
+ while True :
218
+ if nAttempt == __class__ ._C_MAX_GET_PID_ATEMPTS :
219
+ errMsg = "Can't read postmaster pid file [{0}]." .format (pid_file )
220
+ raise Exception (errMsg )
216
221
217
- # for clarity
218
- return 0
222
+ nAttempt += 1
223
+
224
+ s1 = self .status ()
225
+ if s1 != NodeStatus .Running :
226
+ return 0
227
+
228
+ try :
229
+ lines = self .os_ops .readlines (pid_file )
230
+ except Exception :
231
+ s2 = self .status ()
232
+ if s2 == NodeStatus .Running :
233
+ raise
234
+ return 0
235
+
236
+ assert lines is not None # [2025-02-27] OK?
237
+ assert type (lines ) == list # noqa: E721
238
+ if len (lines ) == 0 :
239
+ continue
240
+
241
+ pid_s = lines [0 ]
242
+ assert type (pid_s ) == str # noqa: E721
243
+ if len (pid_s ) == 0 :
244
+ continue
245
+
246
+ pid = int (pid_s )
247
+ return pid
219
248
220
249
@property
221
250
def auxiliary_pids (self ):
0 commit comments