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

Add mechanism to register (noisy) signal handler #2631

Merged
merged 1 commit into from
May 2, 2018

Conversation

sargun
Copy link
Contributor

@sargun sargun commented Apr 5, 2018

This allows programs that are waiting on a signal to proceed to
work. Sometimes, there are programs that are waiting for an alarm,
or similar prior to performing I/O.

This was impossible, because signal handlers were never registered
as noisy actors in asio. This allows for an optional named parameter
to be passed with that context.

A simple example of a program that was not supposed to terminate until it received a signal:

use "signals"
use "bureaucracy"

actor Main
  new create(env: Env) =>
    // Create a TERM handler
    let custodian = Custodian
    let signal = SignalHandler(TermHandler(env, custodian), Sig.int())
    custodian(signal)

class TermHandler is SignalNotify
  let _env: Env
  let _custodian : Custodian

  new iso create(env: Env, custodian : Custodian) =>
    _env = env
    _custodian = custodian

  fun ref apply(count: U32): Bool =>
    _env.out.print("TERM signal received, shutting down")
    _custodian.dispose()
    true

In order to fix this behaviour under the PR, the signal handler can be created like so:

let signal = SignalHandler(TermHandler(env, custodian), Sig.int() where noisy = true)

@mfelsche
Copy link
Contributor

mfelsche commented Apr 5, 2018

That is indeed a nice change!

There is nonetheless one thing i would like to ask of you: Could you describe in a few words the behaviours when noisy is set to true or false in the docstring of the method? It is quite important as it has significant impact on the runtime behavior.

@mfelsche
Copy link
Contributor

mfelsche commented Apr 5, 2018

Also this section should be adapted: https://github.com/ponylang/ponyc/blob/master/packages/signals/sig.pony#L42

@SeanTAllen
Copy link
Member

SeanTAllen commented Apr 5, 2018 via email

@sargun
Copy link
Contributor Author

sargun commented Apr 5, 2018

@SeanTAllen Cancelled by who, the user? If it helps, we can create a whitelist of signals that are allowed to be registered in a noisy way.

@SeanTAllen
Copy link
Member

Yes cancelled by the user.

If its noisy then you need to be able to shut it down so that the program can exit if the signal is never received.

related:

I dont think "noisy" should be the name that is exposed in the API.

@@ -15,14 +15,14 @@ actor SignalHandler
let _sig: U32
var _event: AsioEventID

new create(notify: SignalNotify iso, sig: U32) =>
new create(notify: SignalNotify iso, sig: U32, noisy : Bool = false) =>
Copy link
Member

Choose a reason for hiding this comment

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

Small formatting issue - can you remove the whitespace before the :, to make the code style match the style guide?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@jemc
Copy link
Member

jemc commented Apr 5, 2018

To cancel the noisy listener, I think dispose should already do the trick here, so no more change there should be needed I think (but I haven't verified).

I agree that finding a better name for this than noisy would be good (but currently not sure what to suggest). And I also agree that the consequences of the parameter need to be explained in a docstring for this method.

@jemc
Copy link
Member

jemc commented Apr 5, 2018

Maybe wait is a good name for the parameter, as long as it's explained in the docstring?

Something like:

If the wait parameter is true, the program will not terminate until dispose is called.

@sargun
Copy link
Contributor Author

sargun commented Apr 5, 2018 via email

@slfritchie
Copy link
Contributor

FWIW, my understanding of the word "noisy" is used in opposite of "quiescent" (which appears in scheduler.c). The runtime can shut down if all possible sources of messages are quiet (not sending anything) and will be quiet in the future.

@jemc
Copy link
Member

jemc commented Apr 5, 2018

Yeah, that's where "noisy" comes from, and it does make some sense after realizing the relationship to "quiescence" - it's just a bit of an odd / novel use of the word that I'm not aware of being used in any other programming contexts - perhaps because pony's model for detecting quiescence to terminate the program is also novel (as far as I know - I may be unaware of some prior art here).

@SeanTAllen
Copy link
Member

SeanTAllen commented Apr 5, 2018 via email

@sargun
Copy link
Contributor Author

sargun commented Apr 8, 2018

@jemc Changed to "wait"

@@ -10,19 +10,22 @@ use @pony_asio_event_destroy[None](event: AsioEventID)
actor SignalHandler
"""
Listen for a specific signal.

If the wait parameter is true, the program will not terminate until the SignalHandler's dispose method is called.

Copy link
Member

Choose a reason for hiding this comment

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

This extra empty line isn't needed.

Copy link
Member

@jemc jemc left a comment

Choose a reason for hiding this comment

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

Looks good to me, other than the small empty line comment I made above.

Anybody else have things to say about this?

@slfritchie
Copy link
Contributor

Fine by me. Should this have a changelog+added tag added?

@sargun
Copy link
Contributor Author

sargun commented Apr 10, 2018

@jemc Just removed the whitespace.

1 similar comment
@sargun
Copy link
Contributor Author

sargun commented Apr 10, 2018

@jemc Just removed the whitespace.

@@ -10,19 +10,21 @@ use @pony_asio_event_destroy[None](event: AsioEventID)
actor SignalHandler
"""
Listen for a specific signal.
If the wait parameter is true, the program will not terminate until the SignalHandler's dispose method is called.
Copy link
Contributor

Choose a reason for hiding this comment

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

i would also add: "or if the SignalNotify returns false, after handling the signal"
as this also disposes the SignalHandler and unsubscribes from asio.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that "logic" is already documented in SignalNotify.pony:

    Called with the the number of times the signal has fired since this was
    last called. Return false to stop listening for the signal.

Copy link
Contributor

Choose a reason for hiding this comment

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

True point. I would add it nonetheless, as it is important for users to understand under which circumstances waiting stops. It is more convenient to users to have this information in one place.

Thank you for the PR, it looks very good.

This allows programs that are waiting on a signal to proceed to
work. Sometimes, there are programs that are waiting for an alarm,
or similar prior to performing I/O.

This was impossible, because signal handlers were never registered
as noisy actors in asio. This allows for an optional named parameter
to be passed with that context.
@sargun
Copy link
Contributor Author

sargun commented Apr 10, 2018

@mfelsche Added a bit more info to the docstring

@SeanTAllen SeanTAllen added the changelog - added Automatically add "Added" CHANGELOG entry on merge label May 2, 2018
@SeanTAllen
Copy link
Member

Thanks @sargun

@SeanTAllen SeanTAllen merged commit 2f35bd0 into ponylang:master May 2, 2018
ponylang-main added a commit that referenced this pull request May 2, 2018
dipinhora pushed a commit to dipinhora/ponyc that referenced this pull request Jun 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog - added Automatically add "Added" CHANGELOG entry on merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants