Skip to content

Commit 6152a07

Browse files
Merge pull request #166 from dotflow-io/feature/131
🪲 BUG-#131: Fix timeout thread leak in ThreadPoolExecutor
2 parents a1b3307 + 67587ad commit 6152a07

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

dotflow/core/action.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,32 @@ def _run_action(self, *args, task=None, **kwargs):
144144
for attempt in range(1, self.retry + 1):
145145
try:
146146
if self.timeout:
147-
with ThreadPoolExecutor(max_workers=1) as executor:
147+
executor = ThreadPoolExecutor(max_workers=1)
148+
try:
148149
future = executor.submit(
149150
self._call_func,
150151
is_async,
151152
*args,
152153
**kwargs,
153154
)
154155
result = future.result(timeout=self.timeout)
156+
except TimeoutError:
157+
future.cancel()
158+
executor.shutdown(wait=False, cancel_futures=True)
159+
raise
160+
except Exception:
161+
executor.shutdown(wait=False)
162+
raise
163+
else:
164+
executor.shutdown(wait=False)
155165
else:
156166
result = self._call_func(is_async, *args, **kwargs)
157167

158168
return result
159169

170+
except TimeoutError:
171+
raise
172+
160173
except Exception as error:
161174
last_exception = error
162175

0 commit comments

Comments
 (0)