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

change signal to SIG_DFL after exit_gracefully #3466

Merged
merged 1 commit into from
Jun 14, 2024

Conversation

zrr1999
Copy link
Contributor

@zrr1999 zrr1999 commented Jun 14, 2024

Description

I want to create multiple windows in my program, so I ran multiple app_async functions. However, I found that when I ran multiple app_async functions, Ctrl+C could not terminate my program.

Modifying exit_gracefully to ensure that the default value of the signal is restored after execution seems to be a solution.

Test Code

import asyncio
import flet as ft
import socket


def get_free_port():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("", 0))
    _, port = s.getsockname()
    s.close()
    return port


def main(page):
    page.snack_bar = ft.SnackBar(
        content=ft.Text("Hello, world!"),
        action="Alright!",
    )

    page.add(ft.ElevatedButton("Open SnackBar"))


loop = asyncio.new_event_loop()


async def hello_world():
    loop.create_task(ft.app_async(target=main, port=get_free_port()))
    while True:
        await asyncio.sleep(1)


loop.create_task(hello_world())
loop.run_until_complete(hello_world())

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I signed the CLA.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing tests pass locally with my changes
  • I have made corresponding changes to the documentation (if applicable)

Screenshots (if applicable):

Additional details

Summary by Sourcery

This pull request addresses an issue where the program could not be terminated using Ctrl+C when running multiple app_async functions. The fix involves restoring the default signal handlers for SIGINT and SIGTERM after the exit_gracefully function is executed.

  • Bug Fixes:
    • Restored default signal handlers for SIGINT and SIGTERM after executing the exit_gracefully function to ensure proper termination of the program when running multiple app_async functions.

Copy link
Contributor

sourcery-ai bot commented Jun 14, 2024

Reviewer's Guide by Sourcery

This pull request addresses an issue where multiple instances of the app_async function prevent the program from terminating with Ctrl+C. The solution involves modifying the exit_gracefully function to restore the default signal handlers for SIGINT and SIGTERM after execution.

File-Level Changes

Files Changes
sdk/python/packages/flet-runtime/src/flet_runtime/app.py Modified the exit_gracefully function to reset signal handlers for SIGINT and SIGTERM to their default values after execution to ensure proper termination of the program when multiple app_async functions are running.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @zrr1999 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines +145 to +146
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGTERM, signal.SIG_DFL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential race condition with signal handling.

Reverting the signal handlers to their default behavior within exit_gracefully might introduce a race condition if another signal is received before the program exits. Consider ensuring that the program exits immediately after setting the default handlers.

@FeodorFitsner FeodorFitsner merged commit 1504fba into flet-dev:main Jun 14, 2024
2 checks passed
FeodorFitsner pushed a commit that referenced this pull request Jun 17, 2024
…ies to `Window` and `BrowserContextMenu` classes (#3463)

* initial commit

* commit

* update Window

* create BrowserContextMenu class

* chore: remove deprecation_warning and reformat deprecated

* change signal to SIG_DFL after exit_gracefully (#3466)

* improve type hint for run_task and run_thread (#3459)

* add type hint

* fix Future[RetT]

* feat(map): add missing py-events, better typing (#3464)

* initial commit

* commit

* update Window

* create BrowserContextMenu class

* chore: remove deprecation_warning and reformat deprecated

* Geolocator: rename some classes and methods

---------

Co-authored-by: Zhan Rongrui <[email protected]>
zrr1999 added a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
zrr1999 added a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
…ies to `Window` and `BrowserContextMenu` classes (flet-dev#3463)

* initial commit

* commit

* update Window

* create BrowserContextMenu class

* chore: remove deprecation_warning and reformat deprecated

* change signal to SIG_DFL after exit_gracefully (flet-dev#3466)

* improve type hint for run_task and run_thread (flet-dev#3459)

* add type hint

* fix Future[RetT]

* feat(map): add missing py-events, better typing (flet-dev#3464)

* initial commit

* commit

* update Window

* create BrowserContextMenu class

* chore: remove deprecation_warning and reformat deprecated

* Geolocator: rename some classes and methods

---------

Co-authored-by: Zhan Rongrui <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants