Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Briefcase not closing down cleanly #3266

Closed
JDarsley opened this issue Mar 17, 2025 · 12 comments · Fixed by #3270
Closed

Briefcase not closing down cleanly #3266

JDarsley opened this issue Mar 17, 2025 · 12 comments · Fixed by #3270
Labels
bug A crash or error in behavior. windows The issue relates to Microsoft Windows support.

Comments

@JDarsley
Copy link
Contributor

JDarsley commented Mar 17, 2025

Describe the bug

On Windows, when using the default menu option File | Exit, or the standard window close button, while running a Toga app under Briefcase, the application crashes, reporting that there are still pending operations.

Steps to reproduce

  1. Set up a virtual environment, and create a new project called "jotter".
  2. Copy the two Python files into the src\jotter folder, as app.py and main.py (with a pair of double underscores - they appear to have been formatted out of the latter file name).
  3. briefcase dev
  4. See error messages in console (attached).

app.py

import toga

class Jotter(toga.App):

    def startup(self):
        self.main_window = toga.MainWindow()
        self.main_window.content = toga.Box()

        lstDetail = [
            {"Detail": "Subject Heading"},
            {"Detail": "Date and time"},
            {"Detail": "Note text"}
        ]

        self.tblNoteDetails = toga.Table(
            headings=None,
            accessors=["Detail"],
            data=lstDetail,
            on_select=self.onSelect,
            flex=1
        )

        self.countOnSelect = 0

        self.main_window.content.add(self.tblNoteDetails)
        
        self.main_window.show()

    def onSelect(self, widget, **kwargs):
        self.countOnSelect += 1
        print(f"\nSelect handler - call {self.countOnSelect}")
        print({widget.data[0]})
        print({widget.data[1]})
        print({widget.data[2]})

(Am I doing something wrong with the code formatting? It won't come out right.)

main.py

from jotter.app import Jotter

if __name__ == "__main__":
    app = Jotter()
    app.main_loop()

Expected behavior

Closing the app shouldn't generate error messages. I was previously running an older version of Toga, and didn't get those errors.

Screenshots

Error messages on exit from the app.

Image

Environment

  • Operating System: Windows 11 24H2
  • Python version: 3.12.6
  • Software versions:
    • Briefcase: 0.3.22
    • Toga: 0.5.0
    • ...

Logs


Additional context

No response

@JDarsley JDarsley added the bug A crash or error in behavior. label Mar 17, 2025
@HalfWhitt
Copy link
Contributor

(Am I doing something wrong with the code formatting? It won't come out right.)

It looks like you're using one backtick(`) before and after your code blocks; you should use three instead (```). Alternatively, indent the whole code block by four spaces instead, with no backticks. (This is why everything in your code that's indented once or more currently displays as a code block, with one layer of indentation removed.)

An advantage of using backticks instead of indentation is that you can specify the language and get syntax highlighting, e.g. in this case open a code block with ```python.

@JDarsley
Copy link
Contributor Author

Thanks Charles, that's useful to know.

@HalfWhitt
Copy link
Contributor

I don't have my hands on Windows at the moment to look into this. But if you have the time, here are some things that would help narrow down the source. Do you get the same crash...

  • With one of the Toga example apps (in /examples in the repo)?
  • When running the Python module directly, rather than from Briefcase?
  • With Toga 0.4.9 instead of 0.5.0?

@freakboy3742 freakboy3742 added the windows The issue relates to Microsoft Windows support. label Mar 18, 2025
@freakboy3742
Copy link
Member

Thanks for the report - I've been able to reproduce this using Toga Tutorial 0 on Windows 10. It only occurs with briefcase dev. I don't see the problem on Toga 0.4.9.

I don't see the problem with python -m tutorial; but if I set PYTHODNEVMODE=1 in the environment, I can reproduce the messages. They appear to be a combination of warnings, and asyncio cleanup errors.

The good news is that they're not a problem per se - they're obviously undesirable, but they're not an indication that anything is actually broken. The issues being reported are all things that will be cleaned up automatically when the app exits.

They seem to stem from the end-of-life of the Windows Proactor - that proactor isn't being explicitly shut down, and Python's asyncio cleanup warnings are being triggered as a result. From the look of it, the "run_forever_cleanup" portion of the event loop is not being executed... but that isn't a change since 0.4.9, so I can't work out why this has become a problem. More investigation is required.

@JDarsley
Copy link
Contributor Author

I don't have my hands on Windows at the moment to look into this. But if you have the time, here are some things that would help narrow down the source. Do you get the same crash...

* With one of the Toga example apps (in `/examples` in the repo)?

* When running the Python module directly, rather than from Briefcase?

* With Toga 0.4.9 instead of 0.5.0?

Hi Charles. I think Russell's reply probably covers these points, but if not, let me know if there's anything you'd still like me to try.

@freakboy3742
Copy link
Member

@JDarsley I think I've found a fix for this; if you're able to try out the patch on #3270 that would be very helpful. If you've got a Briefcase project, you can replace the references to toga-core==0.5.0 and toga-winforms==0.5.0 with git+https://github.com/freakboy3742/toga.git@proactor-exit#egg=toga-core&subdirectory=core and git+https://github.com/freakboy3742/toga.git@proactor-exit#egg=toga-winforms&subdirectory=winforms respectively, then running briefcase dev -r to get the updated version installed.

@JDarsley
Copy link
Contributor Author

@freakboy3742 I'm not entirely clear on this. When you refer to the "Briefcase project", I'm guessing that would essentially be the pyproject.toml file? If so, I have a reference in there to toga-winforms:

[tool.briefcase.app.jotter.windows]
requires = [
    "toga-winforms~=0.4.7",
]

No reference to toga-core though. So I'm not sure what changes are needed. I did try simply replacing the toga-winforms requirement with your new version, but predictably, on its own that's clearly not ok.

I've attached the full file, in case that helps (I've given it a .txt extension, as the attachment mechanism here won't allow .toml).

pyproject.toml.txt

As an aside (and nothing to do with attempting to upgrade), the very first run of version 0.3.22 of briefcase dev actually downgrades a number of components - toga-core from 0.5.0 to 0.4.9, toga-winforms the same, and travertino from 0.5.0 to 0.3.0.

@mhsmith
Copy link
Member

mhsmith commented Mar 19, 2025

No reference to toga-core though. So I'm not sure what changes are needed. I did try simply replacing the toga-winforms requirement with your new version, but predictably, on its own that's clearly not ok.

Try replacing the toga-winforms requirement with both the toga-core and toga-winforms URLs above.

the very first run of version 0.3.22 of briefcase dev actually downgrades a number of components - toga-core from 0.5.0 to 0.4.9, toga-winforms the same, and travertino from 0.5.0 to 0.3.0.

Yes, that will be because of the ~=0.4.7 requirement. You can update that to 0.5.0 now that it's been released.

@freakboy3742
Copy link
Member

@freakboy3742 I'm not entirely clear on this. When you refer to the "Briefcase project", I'm guessing that would essentially be the pyproject.toml file? If so, I have a reference in there to toga-winforms:

Apologies - I forgot the content that Briefcase generates by default. I've been using tutorial0 as a demo, and it has a slightly different setup.

If you modify the windows section of your pyproject.toml to read:

[tool.briefcase.app.jotter.windows]
requires = [
    "git+https://github.com/freakboy3742/toga.git@proactor-exit#egg=travertino&subdirectory=travertino",
    "git+https://github.com/freakboy3742/toga.git@proactor-exit#egg=toga-core&subdirectory=core",
    "git+https://github.com/freakboy3742/toga.git@proactor-exit#egg=toga-winforms&subdirectory=winforms",
]

then run briefcase dev -r, that should be everything you need to test the new code.

@JDarsley
Copy link
Contributor Author

Thanks @mhsmith and @freakboy3742. I can confirm that after making those changes, the error messages no longer appear when closing the app.

However, a number of console messages caught my eye. Is there anything here that looks wrong?

Installing collected packages: travertino, toga-core, toga-winforms
  Attempting uninstall: travertino
    Found existing installation: travertino 0.5.0
    Uninstalling travertino-0.5.0:
      Successfully uninstalled travertino-0.5.0
  Attempting uninstall: toga-core
    Found existing installation: toga-core 0.5.0
    Uninstalling toga-core-0.5.0:
      Successfully uninstalled toga-core-0.5.0
  Attempting uninstall: toga-winforms
    Found existing installation: toga-winforms 0.5.0
    Uninstalling toga-winforms-0.5.0:
      Successfully uninstalled toga-winforms-0.5.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
toga 0.5.0 requires toga-winforms==0.5.0; sys_platform == "win32", but you have toga-winforms 0.4.5.dev1514+gdd979716c which is incompatible.
Successfully installed toga-core-0.4.5.dev1514+gdd979716c toga-winforms-0.4.5.dev1514+gdd979716c travertino-0.4.5.dev1514+gdd979716c
Installing dev requirements... done

Obviously, pip is complaining. However, the question for me is whether those versions 0.4.5.dev are correct - as in lower version numbers than the 0.5.0 I was previously running? I'm guessing it's probably the way dev versions feed into the main branch, so probably nothing.

Will it be obvious, when the time comes, when I should update pyproject.toml back to main release versions?

@mhsmith mhsmith changed the title Brriefcase not closing down cleanly Briefcase not closing down cleanly Mar 20, 2025
@mhsmith
Copy link
Member

mhsmith commented Mar 20, 2025

Obviously, pip is complaining. However, the question for me is whether those versions 0.4.5.dev are correct - as in lower version numbers than the 0.5.0 I was previously running? I'm guessing it's probably the way dev versions feed into the main branch, so probably nothing.

Yes, this is fine. It's because the freakboy3742/toga fork doesn't automatically get copies of new version tags from the main repository.

Will it be obvious, when the time comes, when I should update pyproject.toml back to main release versions?

The fix will be included in the next Toga release. You can be notified of this by watching the project on GitHub – select "Custom" mode to be notified only of releases and not other activity.

@JDarsley
Copy link
Contributor Author

Thanks @mhsmith - I'll watch for the release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior. windows The issue relates to Microsoft Windows support.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants