[libcamera-devel,v2,07/13] py: cam.py: exit on exception
diff mbox series

Message ID 20220517143325.71784-8-tomi.valkeinen@ideasonboard.com
State Superseded
Headers show
Series
  • Misc Python bindings patches
Related show

Commit Message

Tomi Valkeinen May 17, 2022, 2:33 p.m. UTC
Catch exceptions in the event_handler, as they would get ignored
otherwise. Print the exception and return False so that the main loop
exits.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/py/cam/cam.py | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

Comments

Kieran Bingham May 17, 2022, 4:52 p.m. UTC | #1
Quoting Tomi Valkeinen (2022-05-17 15:33:19)
> Catch exceptions in the event_handler, as they would get ignored
> otherwise. Print the exception and return False so that the main loop
> exits.
> 
> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  src/py/cam/cam.py | 23 ++++++++++++++---------
>  1 file changed, 14 insertions(+), 9 deletions(-)
> 
> diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py
> index 012b191c..c7da97d7 100755
> --- a/src/py/cam/cam.py
> +++ b/src/py/cam/cam.py
> @@ -11,6 +11,7 @@ import binascii
>  import libcamera as libcam
>  import os
>  import sys
> +import traceback
>  
>  
>  class CustomAction(argparse.Action):
> @@ -286,19 +287,23 @@ def capture_start(contexts):
>  
>  # Called from renderer when there is a libcamera event
>  def event_handler(state):
> -    cm = state['cm']
> -    contexts = state['contexts']
> +    try:
> +        cm = state['cm']
> +        contexts = state['contexts']
>  
> -    os.read(cm.efd, 8)
> +        os.read(cm.efd, 8)
>  
> -    reqs = cm.get_ready_requests()
> +        reqs = cm.get_ready_requests()
>  
> -    for req in reqs:
> -        ctx = next(ctx for ctx in contexts if ctx['idx'] == req.cookie)
> -        request_handler(state, ctx, req)
> +        for req in reqs:
> +            ctx = next(ctx for ctx in contexts if ctx['idx'] == req.cookie)
> +            request_handler(state, ctx, req)
>  
> -    running = any(ctx['reqs-completed'] < ctx['opt-capture'] for ctx in contexts)
> -    return running
> +        running = any(ctx['reqs-completed'] < ctx['opt-capture'] for ctx in contexts)
> +        return running
> +    except Exception as e:
> +        traceback.print_exc()
> +        return False
>  
>  
>  def request_handler(state, ctx, req):
> -- 
> 2.34.1
>

Patch
diff mbox series

diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py
index 012b191c..c7da97d7 100755
--- a/src/py/cam/cam.py
+++ b/src/py/cam/cam.py
@@ -11,6 +11,7 @@  import binascii
 import libcamera as libcam
 import os
 import sys
+import traceback
 
 
 class CustomAction(argparse.Action):
@@ -286,19 +287,23 @@  def capture_start(contexts):
 
 # Called from renderer when there is a libcamera event
 def event_handler(state):
-    cm = state['cm']
-    contexts = state['contexts']
+    try:
+        cm = state['cm']
+        contexts = state['contexts']
 
-    os.read(cm.efd, 8)
+        os.read(cm.efd, 8)
 
-    reqs = cm.get_ready_requests()
+        reqs = cm.get_ready_requests()
 
-    for req in reqs:
-        ctx = next(ctx for ctx in contexts if ctx['idx'] == req.cookie)
-        request_handler(state, ctx, req)
+        for req in reqs:
+            ctx = next(ctx for ctx in contexts if ctx['idx'] == req.cookie)
+            request_handler(state, ctx, req)
 
-    running = any(ctx['reqs-completed'] < ctx['opt-capture'] for ctx in contexts)
-    return running
+        running = any(ctx['reqs-completed'] < ctx['opt-capture'] for ctx in contexts)
+        return running
+    except Exception as e:
+        traceback.print_exc()
+        return False
 
 
 def request_handler(state, ctx, req):