Avaiga / taipy

Turns Data and AI algorithms into production-ready web applications in no time.
https://www.taipy.io
Apache License 2.0
12.5k stars 937 forks source link

[DOCS] How to stop/ shutdown taipy gracefully without a long traceback? #1334

Closed david-fortini closed 2 months ago

david-fortini commented 4 months ago

Issue Description

Right now, every time I close taipy using CTRL+C, a long traceback gets printed. What is the proper way to close the app without having this?

Screenshots or Examples (if applicable)

^C is where the interrupt happens

poetry run taipy run DataCheckDashboard.py
[2024-05-29 10:58:09][Taipy][INFO] Application is running in 'debug' mode
[2024-05-29 10:58:09][Taipy][INFO] 'allow_unsafe_werkzeug' has been set to True
[2024-05-29 10:58:09][Taipy][INFO] 'async_mode' parameter has been overridden to 'threading'. Using Flask built-in development server with debug mode
[2024-05-29 10:58:09][Taipy][INFO]  * Server starting on http://127.0.0.1:2453
/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/subprocess.py:946: ResourceWarning:

subprocess 1302237 is still running

 * Serving Flask app 'Taipy'
 * Debug mode: on
[2024-05-29 10:58:11][Taipy][INFO]  * Server reloaded on http://127.0.0.1:2453
**^C** /home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/subprocess.py:946: ResourceWarning:

subprocess 1302241 is still running

Traceback (most recent call last):
  File "/home/david/miniconda3/envs/onexus_backend_py38/bin/taipy", line 8, in <module>
    sys.exit(_entrypoint())
  File "/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/site-packages/taipy/_entrypoint.py", line 53, in _entrypoint
    _RunCLI.parse_arguments()
  File "/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/site-packages/taipy/_cli/_run_cli.py", line 53, in parse_arguments
    subprocess.run(
  File "/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/subprocess.py", line 495, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/subprocess.py", line 1020, in communicate
    self.wait()
  File "/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/subprocess.py", line 1083, in wait
    return self._wait(timeout=timeout)
  File "/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/subprocess.py", line 1806, in _wait
    (pid, sts) = self._try_wait(0)
  File "/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/subprocess.py", line 1764, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt
/home/david/miniconda3/envs/onexus_backend_py38/lib/python3.8/subprocess.py:946: ResourceWarning: subprocess 1302157 is still running
  _warn("subprocess %s is still running" % self.pid,

Proposed Solution (optional)

Either document how to shut it down gracefully or just show shutting down server without showing traceback.

Code of Conduct

FlorianJacta commented 4 months ago

Great suggestion, I like this issue!

trgiangdo commented 4 months ago

We can try to catch the Exceptions when exiting with CTRL+C. It will be much cleaner, yes

yaten2302 commented 3 months ago

Hi @trgiangdo @FlorianJacta, I want to work on this issue, could you please assign it to me?

yaten2302 commented 3 months ago

Hi @trgiangdo @david-fortini , could you please guide that how can I reproduce this issue? Also, @trgiango, could you please guide that for the catching the Exceptions, I've to make changes in the taipy/cli/ dir, am I right?

david-fortini commented 3 months ago

Hi @yaten2302 , so basically just launch any app and close with CTRL+C.

Issue Description

Right now, every time I close taipy using CTRL+C, a long traceback gets printed. What is the proper way to close the app without having this?

yaten2302 commented 3 months ago

Hi @yaten2302 , so basically just launch any app and close with CTRL+C.

Issue Description

Right now, every time I close taipy using CTRL+C, a long traceback gets printed. What is the proper way to close the app without having this?

Got it @david-fortini 👍 @trgiangdo, could you please guide that in which dir or file the taipy cli is located, like where the catching and all is done?

trgiangdo commented 3 months ago

If you launch a Taipy app using taipy run ... command, then the file that handle the app execution is taipy/_cli/_run_cli.py.

However, if launching Taipy app using python ..., I'm not sure if there is anything we can do on Taipy side.

yaten2302 commented 3 months ago

Got it @trgiangdo 👍, so, like, in the file - taipy/_cli/_run_cli.py, I've to make changes in the handle_command(cls) function? I was just checking, it's given like this (the handle_command func):

def handle_command(cls):
        args, _ = _TaipyParser._parser.parse_known_args()
        if getattr(args, "which", None) != "run":
            return

        all_args = sys.argv[3:]  # First 3 args are always (1) Python executable, (2) run, (3) Python file

        external_args = []
        try:
            external_args_index = all_args.index("external-args")
        except ValueError:
            pass
        else:
            external_args.extend(all_args[external_args_index + 1 :])
            all_args = all_args[:external_args_index]

        taipy_args = [f"--taipy-{arg[2:]}" if arg.startswith("--") else arg for arg in all_args]

        subprocess.run(
            [sys.executable, args.application_main_file, *(external_args + taipy_args)],
            stdout=sys.stdout,
            stderr=sys.stdout,
        ) 

        sys.exit(0)

So, will it help by removing the stdout=sys.stdout in the subprocess.run(...), because ig on executing the subprocess.run(...), the stdout is only which is getting logged to the cli?

trgiangdo commented 3 months ago

If removing stdout, all errors will be lost right? I don't think we want that

yaten2302 commented 3 months ago

Yes @trgiangdo , we don't want that. I thought that the errors are logged from the stderr=sys.stdout? Is it like that? Like, I was just reading the doc of sys, it's showing that stderr is used to capture any exception that occurs, is it like that or by removing the stdout, it'll remove all the errors as well?

trgiangdo commented 3 months ago

You can test it out. Either way, the behavior that we want is:

yaten2302 commented 3 months ago

You can test it out. Either way, the behavior that we want is:

  • If the application has any error, exceptions, etc, it needs to be shown in the stdout
  • Except from KeyboardInterrupt exception, we will exit without showing the error of that specific exception

@trgiangdo, could you please guide that how can I test it? Like what command do I've to run?

trgiangdo commented 3 months ago

You can launch any app using the taipy run .. command as in https://docs.taipy.io/en/latest/manuals/cli/run/, then CTRL+C to see if it works or not

yaten2302 commented 3 months ago

You can launch any app using the taipy run .. command as in https://docs.taipy.io/en/latest/manuals/cli/run/, then CTRL+C to see if it works or not

@trgiangdo, but by this method, will I be able to test my changes? Because, let's say that I initialized a taipy project, but then, all the dependencies installed will be those present in the official taipy repo and not mine? Is it like that or am I going wrong somewhere?

trgiangdo commented 3 months ago

Your environment needs to have Taipy installed from your local taipy repository so that the change may take effect

yaten2302 commented 3 months ago

Your environment needs to have Taipy installed from your local taipy repository so that the change may take effect

@trgiangdo, could you please guide that how can I install my local taipy repo into a project?

trgiangdo commented 3 months ago

You can follow the guide for pip regular local project installation here: https://pip.pypa.io/en/stable/topics/local-project-installs/#regular-installs

yaten2302 commented 3 months ago

Hey @trgiangdo, I tried to change the handle_command function, but I'm having some issues, could you please guide that where am I going wrong?
image

Also, when I'm running this command - C:/Python312/python.exe main.py, it's not showing any KeyboardInterrupt error, but, when I'm running this one - taipy run main.py, it's showing the KeyboardInterrupt error? Could you please guide that where am I going wrong?

trgiangdo commented 3 months ago

I don't know about this, sorry. Apparently it's not as simple as just catching the KeyboardInterrupt error.

We need to find a better way of doing this

yaten2302 commented 3 months ago

Hey @trgiangdo, actually, when I'm trying to run this command - py -m pip install path/to/taipy, it's showing this error, could you please guide how to fix this:

Could not install packages due to an OSError: [WinError 2] The system cannot find the file specified: 'C:\\Python312\\Scripts\\taipy.exe' -> 'C:\\Python312\\Scripts\\taipy.exe.deleteme'

image

trgiangdo commented 3 months ago

I found this stackoverflow thread that could resolve your problem: https://stackoverflow.com/questions/66322049/could-not-install-packages-due-to-an-oserror-winerror-2-no-such-file-or-direc

yaten2302 commented 3 months ago

Hey @trgiangdo, actually I was working on this issue for a few days, I took help on some discord servers as well and what I found is that this might solve this issue?

try:
       subprocess.run(
                [sys.executable, args.application_main_file, *(external_args + taipy_args)],
                stdout=sys.stdout,
                stderr=sys.stdout,
            )
     except KeyboardInterrupt:
              sys.exit(0)

Could you please check that if it's working for you or not, because on my side ig there's some problem with installing the project locally? If not, then maybe I'll try some other method?

trgiangdo commented 3 months ago

I tried this approach and on my previous comment

Apparently it's not as simple as just catching the KeyboardInterrupt error. We need to find a better way of doing this

There are also some other errors that are raised, so the log is not really clean yet

yaten2302 commented 3 months ago

I tried this approach and on my previous comment

Apparently it's not as simple as just catching the KeyboardInterrupt error. We need to find a better way of doing this

There are also some other errors that are raised, so the log is not really clean yet

@trgiangdo is the Keyboard Interrupt error still there, like even after using the try except?

trgiangdo commented 3 months ago

yes it's still there along side some other errors

yaten2302 commented 3 months ago

yes it's still there along side some other errors

That's weird! @trgiangdo, maybe I'll try to ask regarding this issue in the Taipy discord server? If at this point it's difficult to fix this issue, then maybe we can put this on a hault and try fixing the other existing issues. I'll also keep checking about it how to fix this👍

github-actions[bot] commented 2 months ago

This issue has been labelled as "🥶Waiting for contributor" because it has been inactive for more than 14 days. If you would like to continue working on this issue, please add another comment or create a PR that links to this issue. If a PR has already been created which refers to this issue, then you should explicitly mention this issue in the relevant PR. Otherwise, you will be unassigned in 14 days. For more information please refer to the contributing guidelines.

github-actions[bot] commented 2 months ago

This issue has been unassigned automatically because it has been marked as "🥶Waiting for contributor" for more than 14 days with no activity.

PrathamGupta06 commented 2 months ago

@trgiangdo #1595 fixes this issue. Kindly have a look at the PR.