swagger-api / swagger-api/swagger-codegen

Exception by __del__

Open
#8,504 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

https://github.com/swagger-api/swagger-codegen/blob/dbd0a4bb535de0d587c9ccec1465a25c8288ee96/modules/swagger-codegen/src/main/resources/python/api_client.mustache#L72-L74

The code causes an exception in generated api client, when an async api call is made. Take mine for example:

[DEBUG/MainThread:140428243785472] added worker
[DEBUG/MainThread:140428243785472] added worker
...
[INFO/Dummy-6:140428243785472] process shutting down
[DEBUG/Dummy-6:140428243785472] running all "atexit" finalizers with priority >= 0
[DEBUG/Dummy-6:140428243785472] finalizing pool
[DEBUG/Dummy-6:140428243785472] helping task handler/workers to finish
[DEBUG/Dummy-6:140428243785472] joining worker handler
[DEBUG/Thread-1:140428186494720] worker got sentinel -- exiting
[DEBUG/Thread-1:140428186494720] worker exiting after 48 tasks
[DEBUG/Thread-5:140428152923904] result handler found thread._state=TERMINATE
[DEBUG/Thread-5:140428152923904] result handler exiting: len(cache)=0, thread._state=2
[DEBUG/Thread-2:140428178102016] worker got sentinel -- exiting
[DEBUG/Thread-2:140428178102016] worker exiting after 48 tasks
[DEBUG/Thread-3:140428169709312] worker handler exiting
[DEBUG/Thread-4:140428161316608] task handler got sentinel
[DEBUG/Dummy-6:140428243785472] joining task handler
[DEBUG/Thread-4:140428161316608] task handler sending sentinel to result handler
[DEBUG/Thread-4:140428161316608] task handler sending sentinel to workers
[DEBUG/Thread-4:140428161316608] task handler exiting
[DEBUG/Thread-4:140428161316608] Api client is closing! <-- This is my mark!
[DEBUG/Thread-4:140428161316608] closing pool
[DEBUG/Thread-4:140428161316608] joining pool
Exception ignored in: <bound method ApiClient.__del__ of <hpc_acm.api_client.ApiClient object at 0x7fb7fc518748>>
Traceback (most recent call last):
  File "/home/.../api_client.py", line 82, in __del__
    self.pool.join()
  File "/usr/lib/python3.5/multiprocessing/pool.py", line 511, in join
    self._task_handler.join()
  File "/usr/lib/python3.5/threading.py", line 1051, in join
    raise RuntimeError("cannot join current thread")
RuntimeError: cannot join current thread
[DEBUG/Dummy-6:140428243785472] joining result handler
[DEBUG/Dummy-6:140428243785472] running the remaining "atexit" finalizers

My marked code is:

    def __del__(self):
        from multiprocessing import util
        util.debug('Api client is closing!')
        self.pool.close()
        self.pool.join()

And this is the code snippet from /usr/lib/python3.5/multiprocessing/pool.py:

555         util.debug('joining task handler')
556         if threading.current_thread() is not task_handler:
557             task_handler.join()
558
559         util.debug('joining result handler')
560         if threading.current_thread() is not result_handler:
561             result_handler.join()

The __del__ method is expected to run in main thread but here clearly Python runs it on a thread which is task_handler. It's the thread that __del__ runs on, and it makes a thread try to join itself and thus throw an exception.

I suggest either try to ignore the exception, or make it a normal "close" method to be called by user manually.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/swagger-codegen/src/main/resources/python/api_client.mustache at lines 72-74 and compare the generated ApiClient.del with the reported Python 3.5 traceback. Reproduce an asynchronous API call during shutdown and verify that cleanup no longer attempts to join the current task-handler thread. The issue suggests either safely ignoring the exception or exposing cleanup as an explicit close method.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.