From 70e9500c93dfbdb9bcd34678c6d85f2825e6f200 Mon Sep 17 00:00:00 2001
From: Lukas Schauer <lukas@schauer.so>
Date: Thu, 30 Apr 2020 20:47:39 +0200
Subject: [PATCH] close mkstemp filehandlers...

---
 videos/templates/worker.py | 39 ++++++++++++++++++++------------------
 1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/videos/templates/worker.py b/videos/templates/worker.py
index 4a7890c..9e7df70 100644
--- a/videos/templates/worker.py
+++ b/videos/templates/worker.py
@@ -36,9 +36,9 @@ def render_master(task, tmpfiles=[]):
     authHandler = urllib.request.HTTPBasicAuthHandler(manager)
     opener = urllib.request.build_opener(authHandler)
     urllib.request.install_opener(opener)
-    urllib.request.urlretrieve(origfileurl, tmpfiles[0])
+    urllib.request.urlretrieve(origfileurl, tmpfiles[0][1])
 
-    meta = json.loads(subprocess.check_output(["ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", tmpfiles[0]]))
+    meta = json.loads(subprocess.check_output(["ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", tmpfiles[0][1]]))
 
     video_streams = list([stream for stream in meta['streams'] if stream['codec_type'] == 'video'])
     audio_streams = list([stream for stream in meta['streams'] if stream['codec_type'] == 'audio'])
@@ -76,7 +76,7 @@ def render_master(task, tmpfiles=[]):
 
     ffmpeg_command  = ['ffmpeg', '-y', '-v', 'info', '-nostats']
     ffmpeg_command += ['-analyzeduration', '20000000']
-    ffmpeg_command += ['-i', tmpfiles[0]]
+    ffmpeg_command += ['-i', tmpfiles[0][1]]
 
     if task['notranscode']:
         log(task, "notranscode flag is set, only remuxing video+audio")
@@ -103,7 +103,7 @@ def render_master(task, tmpfiles=[]):
             ffmpeg_command += ['-map', '[s]']
             ffmpeg_command += ['-c:v:1', 'libx264', '-threads', '4', '-pix_fmt', 'yuv420p', '-crf:v:1', '23', '-profile:v:1', 'high', '-level:v:1', '4.1', '-tune:v:1', 'stillimage', '-disposition:v:1', 'descriptions', '-metadata:s:v:1', 'title="Slides"']
 #    ffmpeg_command += ['-aspect', '16:9']
-    ffmpeg_command += ['-movflags', 'faststart', '-f', 'mp4', tmpfiles[1]]
+    ffmpeg_command += ['-movflags', 'faststart', '-f', 'mp4', tmpfiles[1][1]]
     log(task, repr(ffmpeg_command))
 
     process = subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
@@ -128,20 +128,20 @@ def render_master(task, tmpfiles=[]):
     upload_cmd = ["curl", "-v", "{}/api/upload?key={}".format(SERVER, WORKER_KEY)]
     upload_cmd += ["-F", "type=master"]
     upload_cmd += ["-F", "video={}".format(task['video'])]
-    upload_cmd += ["-F", "videofile=@{}".format(tmpfiles[1])]
+    upload_cmd += ["-F", "videofile=@{}".format(tmpfiles[1][1])]
 
     try:
         log(task, "Generating thumbnail image...")
         thumbnail_command = ['ffmpeg', '-y', '-v', 'info', '-nostats']
         thumbnail_command += ['-analyzeduration', '20000000']
-        thumbnail_command += ['-i', tmpfiles[1]]
+        thumbnail_command += ['-i', tmpfiles[1][1]]
         thumbnail_command += ['-vframes', '1', '-an', '-ss', '5']
-        thumbnail_command += [tmpfiles[2]]
+        thumbnail_command += [tmpfiles[2][1]]
         process = subprocess.Popen(thumbnail_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
         stdout, stderr = process.communicate()
         log(task, stderr.decode())
-        if process.poll() == 0 and os.path.getsize(tmpfiles[2]) > 0:
-            upload_cmd += ["-F", "thumbnail=@{}".format(tmpfiles[2])]
+        if process.poll() == 0 and os.path.getsize(tmpfiles[2][1]) > 0:
+            upload_cmd += ["-F", "thumbnail=@{}".format(tmpfiles[2][1])]
         else:
             raise Exception("Non-Zero exit status from ffmpeg")
     except:
@@ -171,22 +171,25 @@ while True:
     elif task['type'] == 'master':
         extension = task['path'][::-1].split('.', 1)[0][::-1]
         tmpfiles = [
-            tempfile.mkstemp(suffix=".{}".format(extension))[1],
-            tempfile.mkstemp(suffix=".mp4")[1],
-            tempfile.mkstemp(suffix=".jpg")[1]
+            tempfile.mkstemp(suffix=".{}".format(extension)),
+            tempfile.mkstemp(suffix=".mp4"),
+            tempfile.mkstemp(suffix=".jpg")
         ]
         try:
             render_master(task, tmpfiles)
-            os.unlink(tmpfiles[0])
-            os.unlink(tmpfiles[1])
-            os.unlink(tmpfiles[2])
+            for i in range(len(tmpfiles)):
+                try:
+                    os.close(tmpfiles[i][0])
+                    os.unlink(tmpfiles[i][1])
+                except:
+                    traceback.print_exc()
         except:
             error = traceback.format_exc()
             log(task, "An error occured during processing", failed=True)
             log(task, error)
             log(task, "Recovery-Files for manual processing by admin:")
-            log(task, " Source: {}".format(tmpfiles[0]))
-            log(task, " Destination: {}".format(tmpfiles[1]))
-            log(task, " Thumbnail: {}".format(tmpfiles[2]))
+            log(task, " Source: {}".format(tmpfiles[0][1]))
+            log(task, " Destination: {}".format(tmpfiles[1][1]))
+            log(task, " Thumbnail: {}".format(tmpfiles[2][1]))
     else:
         print("Unknown task, ignoring")
-- 
GitLab