Skip to content

add semantics for stopping tasks that have no start handler #77

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

Merged
merged 4 commits into from
Oct 26, 2020

Conversation

tomerd
Copy link
Contributor

@tomerd tomerd commented Oct 9, 2020

motivation: address shutdown for tasks which allocate resources on construction. before this change, only tasks that have been started are shutdown, this creates an issue when the resources are allocated on construction since the item may not get started at all (for example due to error in earlier task) and thus will never be shutdown and the resources will leak.

changes: add semantics to request shutdown even if not started, and assume this se semantics when registering with start: .none or with registerShutdown

@tomerd tomerd requested review from yim-lee, fabianfett and ktoso October 9, 2020 23:14
motivation: address shutdown for tasks which allocate resources on construction. before this change, only tasks that have been started are shutdown, this creates an issue when the resources are allocated on construction since the item may not get started at all (for example due to error in earlier task) and thus will never be shutdown and the resources will leak.

changes: add sematics to request shutdown even if not started, and assume this sematics when registering with start: .none or with registerShutdown
@tomerd tomerd changed the title add sematics for stopping tasks that have no start handler add semantics for stopping tasks that have no start handler Oct 9, 2020
lifecycle.register(label: item5.label, start: .none, shutdown: .sync(item5.shutdown))

let item6 = DestructionSensitive(label: "6", sempahore: sempahore)
lifecycle.register(label: item6.label, shutdownIfNotStarted: true, start: .sync(item3.start), shutdown: .sync(item6.shutdown))
Copy link
Contributor

Choose a reason for hiding this comment

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

Mixed feelings here tbh.

I would love to rather revisit the design I proposed a while ago which allows the shutdown to get values from the start. This way, if there was no start called there is no start value.

I.e.

// start, semantically equivalent to:  
//     () -> SomeValue / EventLoopFuture<SomeValue>
// shutdown, semantically equivalent to: 
//    (TaskStartResult<SomeValue, Error>) -> Void / EventLoopFuture<Void>
lifecycle.register(
    label: item6.label, 
    start: .sync(DestructionSensitive()), 
    shutdown: .sync { $0.success?.shutdown() }
)

etc... Something worth exploring? Should I prototype it or would you have a look @tomerd ?
I remain very worried about those "allocate object outside" and then mutate start/stop the thing from lifecycle callbacks without a communication channel between then somehow... (esp if threading was involved which we might get to because: #21 )

Copy link
Contributor Author

@tomerd tomerd Oct 12, 2020

Choose a reason for hiding this comment

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

absolutely @ktoso - you are more than welcome to explore and prototype and alternative better design. note that in this case the register function will also need to return the instance that is created in start since it is needed "outside" as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yeah I see could be tricky... don't treat this comment as blocker -- I'll try to poke around if I could make it happen

Copy link
Member

@fabianfett fabianfett left a comment

Choose a reason for hiding this comment

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

I think this is really good change, that will make live in many use cases much simpler. 👍🚀 Left some comments here and there for discussion.

Before we merge we should definitely make the distinction between use cases (and in what "modes" ServiceLifecycle can be used) more visible in documentation.

@tomerd
Copy link
Contributor Author

tomerd commented Oct 15, 2020

@fabianfett simplified based on discussion, ptal

@ktoso
Copy link
Contributor

ktoso commented Oct 22, 2020

Admitting I did not have time to loop back here... Feel free to merge if you want to @tomerd

@tomerd tomerd merged commit a7356e1 into swift-server:main Oct 26, 2020
@tomerd tomerd added this to the 1.0.0-alpha.6 milestone Oct 26, 2020
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.

4 participants