Indefinite hanging of execution
See original GitHub issueIt appears that the combined use of multithreading + requests + worker clients can cause an indefinite hanging of execution.
Setup
Running dask-scheduler
with two dask-worker tcp://10.0.0.13:8786 --nthreads 1
workers. I can also reproduce with 2 threads so I don’t think this is super relevant, but you never know.
Code
Apologies for the rather complicated reproducible example.
import requests
import threading
import time
from distributed import Client, worker_client
class Heartbeat(threading.Timer):
def run(self):
self.finished.wait(self.interval)
while not self.finished.is_set():
self.function(*self.args, **self.kwargs)
self.finished.wait(self.interval)
def heartbeat():
print('=' * 50)
print('HEARTBEAT')
requests.post("http://www.google.com")
print('=' * 50)
def sleeper(secs):
timer = Heartbeat(5.0, heartbeat)
timer.daemon = True
timer.start()
for n in range(1, secs):
requests.post("https://www.google.com")
if not timer.is_alive():
raise ValueError("heartbeat died!!")
timer.cancel()
timer.join()
def run():
with worker_client() as c:
futs = [c.submit(sleeper, i) for i in range(3, 10)]
return c.gather(futs)
client = Client("tcp://10.0.0.13:8786")
result = client.gather(client.submit(run))
print('COMPLETE')
Symptoms
Whenever I run this code, it appears that:
- the execution never terminates
- at least one task usually continues heartbeating for a while (as evidenced by
print
statements) - eventually, even this stops and I see:
==================================================
HEARTBEAT
with no ending print
- using something like
time.sleep
as the submitted function doesn’t reproduce the issue - using
separate_thread=False
in theworker_client
appears to fix this particular example, but “in the wild” I’m told this leads to a scheduler deadlock
This could could be a requests
bug, but to be honest I’m not sure how to even begin digging deeper into this, or whether there’s a patch I could implement to avoid this hang.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Hanging - Wikipedia
Hanging is a common method of suicide in which a person applies a ligature to the neck and brings about unconsciousness and then...
Read more >Singapore: indefinite stay of execution for man with learning ...
It ordered an indefinite stay of execution, according to Nagaenthran's lawyer M Ravi. The case has caused global outrage, with Singaporean ...
Read more >Death By Hanging: What Saddam Faced - ABC News
Dec. 29, 2006 -- Saddam Hussein was executed by hanging after his death sentence was upheld by an Iraqi appeals court this...
Read more >Execution by Hanging Still Happens in the U.S. -- But Is It ...
In 1993, Washington State executed Westley Allan Dodd, a convicted murderer of three children. He was hanged -- one of only three ...
Read more >Upcoming Executions - Death Penalty Information Center
Last updated on December 19, 2022 (Dates are subject to change as a result of stays and appeals.) Executions Scheduled for 2022 Month...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I honestly have no idea. My guess is that this will come down to some hidden state within requests. But I would only put the odds of that at around 30-40%
Another approach would be to try to avoid threads altogether. I imagine that this is just a toy example for something that is more complex, but if you really wanted to post something somewhere at a regular interval I would probably use a tornado or asyncio HTTPClient rather than requests so that everything could live on the same thread. That tends to make everything safer.
You already know this, so I suspect that you genuinely do need a thread, but I thought I’d mention it anyway.
OK so it appears that replacing my
Heartbeat
class withAppears to prevents the hang, at the cost of losing the ability to “cancel” or kill the background thread while it’s sleeping.
What would make the
ThreadPoolExecutor
better than utilizing a Thread directly? Was my subclassedTimer
poorly designed? Apologies for the questions, I’m just trying to figure out what to take away / learn from this.