GaretJax / django-click

Write Django management command using the click CLI library
MIT License
251 stars 20 forks source link

Raising Abort Causes Stacktrace Instead of Clean Shutdown. #39

Open peacememories opened 2 years ago

peacememories commented 2 years ago

When either sending a SIGINT or using raise click.Abort, I would expect the application to shut down cleanly with the appropriate error code. Instead, I get the following stack trace:

Traceback (most recent call last):
  File "./manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/djclick/adapter.py", line 68, in run_from_argv
    exit_code = self.main(
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.8/site-packages/djclick/adapter.py", line 50, in invoke
    return super(DjangoCommandMixin, self).invoke(ctx)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/srv/service/vite_frontend/management/commands/frontend.py", line 157, in dev
    f.write(f"{app.name}\n{process.pid}\n")
  File "/usr/local/lib/python3.8/contextlib.py", line 120, in __exit__
    next(self.gen)
  File "/srv/service/vite_frontend/management/commands/frontend.py", line 70, in run_vite
    yield process
  File "/usr/local/lib/python3.8/contextlib.py", line 120, in __exit__
    next(self.gen)
  File "/srv/service/vite_frontend/management/commands/frontend.py", line 47, in run_in_app
    raise click.Abort()

We have worked around this by instead throwing a SystemExit exception, but it would be nice to be able to use Abort