Repository: opencv
Issue: Window stops updating
Labels: priority: low,category: highgui-gui,needs investigation,category: 3rdparty

##### System information (version)

- OpenCV => 4.2
- Operating System / Platform => Ubuntu 18.04
- Compiler => gcc

##### Detailed description

The named window that I create and am using to display images eventually stops updating. No errors are thrown and the call for imshow and waitkey still go through as normal, but the window becomes unresponsive; it ceases to update the image, no mouse events are returned in the callback, and waitKey() & 0xff always returns 255. The main loop continues to run, I have an audio cue that plays when the cameras detect a person and that cue keeps happening whenever I step in front. I have tried increasing the time inside waitKey, but it has no noticeable effect. 

This issue only began after I began really pushing the cpu and gpu on the computer. I am limited in what I can disclose here since this is a proprietary project and not something I'm doing for myself at home. There are two cameras hooked up to the computer and each one has two threads: one to read images from the cameras into a circular buffer, and one that uses the dnn module with cuda backend to detect people. There is also a separate thread that uses the playsound module to play an audio queue. 

Now the weird thing is that this issue only happens when the sound thread plays a sound, if I don't trigger the audio then the whole thing will run forever without a problem; it's as if the sound playing is the last straw that breaks the system. To be clear, the audio isn't the sole cause, that sound thread has always been around and didn't cause any issue until I started to stress the cpu by running the additional threads for the cameras (reading frames and running the dnn nets used to all be done in the main thread).

All cpu cores and the gpu are running between 50-80% on the activity monitor when this happens.

I understand that this will be difficult to reproduce without the code and only general details about the particular setup. If I can come up with some other non-proprietary code that can replicate this issue on my home laptop I will edit this post with that information and my computer specs.

##### Steps to reproduce

```py
import threading
import cv2
import time
import playsound as ps

# busywork thread
stop = False;
g_image = None;
def busywork():
	global stop, g_image;
	while not stop:
		temp = cv2.rotate(g_image, cv2.ROTATE_90_CLOCKWISE);

# beeper thread
def beeper():
	global stop;
	timer = time.time();
	interval = 0.25;

	# beep every interval seconds
	while not stop:
		if time.time() - timer > interval:
			print("Beep");
			ps.playsound("Sound/short_beep.mp3");
			timer = time.time();

def main():
	# grab global flags
	global stop, g_image;

	# create cap
	cap = cv2.VideoCapture(-1);
	_, g_image = cap.read();

	# begin busywork
	threads = [];
	for a in range(25):
		threads.append(threading.Thread(target=busywork));
		threads[-1].start();

	# start beeper thread
	threads.append(threading.Thread(target=beeper));
	threads[-1].start();

	# display loop
	done = False;
	sanity_timer = 0;
	while not done:
		# get frame
		_, frame = cap.read();

		# show
		cv2.imshow("display", frame);
		keypress = cv2.waitKey(1) & 0xff;
		done = keypress == ord('q');

		# check for keypress
		if keypress == ord('a'):
			print("I'M ALIVE!");

		# main loop still going?
		if time.time() - sanity_timer > 5:
			print("Main thread is still going");
			sanity_timer = time.time();

	# stop threads
	stop = True;
	print("Stopped");

	# join
	for a in range(len(threads)):
		threads[a].join();

if __name__ == "__main__":
	main();
```

The number of busywork threads might need to be increased on a different computer. Decreasing the interval on the beeper causes the issue to show up faster, but when it'll happen is still very random. Note: that sanity_timer check will always trigger even after the display and waitkey stop responding.

##### Edits
- I notice that calling destroyAllWindows() after the window freezes causes a "Warning: Source ID ### was not found when attempting to remove it"