Skip to content

Commit e168acf

Browse files
committed
Correctly shutdown HTTP thumbnail server, and find and available port (this fixes freezing unit tests on shutdown)
1 parent 607d7e6 commit e168acf

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/classes/thumbnail.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import os
2929
import re
3030
import openshot
31+
import socket
3132
from threading import Thread
3233
from classes import info
3334
from classes.query import File
@@ -75,18 +76,23 @@ class httpThumbnailServerThread(Thread):
7576
""" This class runs a HTTP thumbnail server inside a thread
7677
so we don't block the main thread with handle_request()."""
7778

79+
def find_free_port(self):
80+
"""Find the first available socket port"""
81+
s = socket.socket()
82+
s.bind(('', 0))
83+
return s.getsockname()[1]
84+
7885
def kill(self):
7986
self.running = False
87+
self.thumbServer.shutdown()
8088

8189
def run(self):
8290
self.running = True
8391

84-
# TODO: Choose availble port
85-
self.server_address = ('127.0.0.1', 8081)
92+
# Start listening for HTTP requests (and check for shutdown every 0.5 seconds)
93+
self.server_address = ('127.0.0.1', self.find_free_port())
8694
self.thumbServer = httpThumbnailServer(self.server_address, httpThumbnailHandler)
87-
88-
while self.running:
89-
self.thumbServer.handle_request()
95+
self.thumbServer.serve_forever(0.5)
9096

9197

9298
class httpThumbnailHandler(BaseHTTPRequestHandler):

0 commit comments

Comments
 (0)