billiard icon indicating copy to clipboard operation
billiard copied to clipboard

return in finally can swallow exceptions

Open iritkatriel opened this issue 1 year ago • 1 comments

In three places in your codebase, you have a return statement in a finally block, which would swallow any in-flight exception:

https://github.com/celery/billiard/blob/366ac5f9a708a64fc48f94da465189f9309288ab/billiard/connection.py#L349 https://github.com/celery/billiard/blob/366ac5f9a708a64fc48f94da465189f9309288ab/billiard/connection.py#L351 https://github.com/celery/billiard/blob/366ac5f9a708a64fc48f94da465189f9309288ab/billiard/pool.py#L1856

This means that if a BaseException (such as KeyboardInterrupt) is raised from the try body, or any exception is raised from an except: clause, it will not propagate on as expected.

See also https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions.

iritkatriel avatar Oct 22 '24 18:10 iritkatriel

thanks for the report! it is a very old code base, we will try to clean it asap

auvipy avatar Mar 20 '25 15:03 auvipy

I upgraded my app to use the new Python 3.14 and I see the following messages in the terminal when start my Django app in dev mode:

/.venv/lib/python3.14/site-packages/billiard/pool.py:1856: SyntaxWarning: 'return' in a 'finally' block
return self._send_ack(
/.venv/lib/python3.14/site-packages/billiard/connection.py:349: SyntaxWarning: 'return' in a 'finally' block
return f
/.venv/lib/python3.14/site-packages/billiard/connection.py:351: SyntaxWarning: 'return' in a 'finally' block
return self._get_more_data(ov, maxsize)

imakecodes avatar Oct 11 '25 19:10 imakecodes

This issue can also be observed when launching celery as a task runner for flask.

/code/.venv/lib/python3.14/site-packages/billiard/pool.py:1856: SyntaxWarning: 'return' in a 'finally' block
  return self._send_ack(
/code/.venv/lib/python3.14/site-packages/billiard/connection.py:349: SyntaxWarning: 'return' in a 'finally' block
  return f
/code/.venv/lib/python3.14/site-packages/billiard/connection.py:351: SyntaxWarning: 'return' in a 'finally' block
  return self._get_more_data(ov, maxsize)
/code/.venv/lib/python3.14/site-packages/celery/concurrency/asynpool.py:409: SyntaxWarning: 'return' in a 'finally' block
  return remove(fd)
 
 -------------- celery@2950dba2997b v5.5.3 (immunity)
--- ***** ----- 
-- ******* ---- Linux-6.6.87.2-microsoft-standard-WSL2-x86_64-with-glibc2.41 2025-10-15 04:24:36
- *** --- * --- 
- ** ---------- [config]
- ** ---------- .> app:         factory:0x751fc3ddd400
- ** ---------- .> transport:   redis://redis:6379/0
- ** ---------- .> results:     redis://redis:6379/0
- *** --- * --- .> concurrency: 20 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

ivis-kuroda avatar Oct 15 '25 04:10 ivis-kuroda

@auvipy do you need help to apply the changes?

danilogco avatar Nov 13 '25 15:11 danilogco

@auvipy do you need help to apply the changes?

feel free to send a PR, please!

auvipy avatar Nov 13 '25 16:11 auvipy

I just made #438 to make this compatible with Python 3.14 (which disapproves of 'return' inside of 'finally' blocks). I guess that should also fix this issue?

torarvid avatar Nov 24 '25 09:11 torarvid