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

When the App stops, the ctx in the afterStop callback is not available #3226

Open
shuqingzai opened this issue Mar 9, 2024 · 3 comments · May be fixed by #3228
Open

When the App stops, the ctx in the afterStop callback is not available #3226

shuqingzai opened this issue Mar 9, 2024 · 3 comments · May be fixed by #3228
Labels
bug Something isn't working

Comments

@shuqingzai
Copy link

What happened:

afterStopFn 接收基于 App 的 ctx 衍生的 sctx , 但是在接收停止信号时,就会取消 App 的 ctx ,导致 sctx 也会被取消,afterStopFn 无法执行(如:需要推送系统停止的消息等)

参考:

是否应该像 stopSrv 的方式,可设置应用停止后,回调函数可设置执行超时,基于 o.ctx 创建,不依赖 App.ctx ??

kratos/app.go

Line 105 in 50cad79

stopCtx, cancel := context.WithTimeout(NewContext(a.opts.ctx, a), a.opts.stopTimeout)

What you expected to happen:

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know?:

Environment:

  • Kratos version (use kratos -v):
  • Go version (use go version):
  • OS (e.g: cat /etc/os-release):
  • Others:
@shuqingzai shuqingzai added the bug Something isn't working label Mar 9, 2024
@kratos-ci-bot kratos-ci-bot changed the title App 停止时,afterStop 回调中的 ctx 不可用 When the App stops, the ctx in the afterStop callback is not available Mar 9, 2024
Copy link

dosubot bot commented Mar 9, 2024

你好 @shuqingzai! 我是 Dosu,一个可以帮助解决bug、回答问题并成为贡献者的机器人。我在这里可以为你提供帮助,同时等待人类维护者的回复。我会立即查看你的问题,并尽快给你一个完整的回答。

@kratos-ci-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Hello @shuqingzai! I'm Dosu, a bot that can help solve bugs, answer questions, and become a contributor. I'm here to help you while I wait to hear back from the human maintainers. I will review your question immediately and give you a complete answer as soon as possible.

Copy link

dosubot bot commented Mar 9, 2024

Based on your description, it seems like you're encountering an issue where the afterStopFn in the Kratos app is being cancelled when the stop signal is received. This is because the afterStopFn is currently set to execute based on the App's ctx, which gets cancelled when the stop signal is received.

You're correct in your suggestion that the afterStopFn could be set to execute based on o.ctx creation, instead of being dependent on App.ctx. This would prevent it from being cancelled when the stop signal is received.

In the current implementation of the Run method in app.go, the afterStop functions are executed with sctx which is derived from a.ctx:

sctx := NewContext(a.ctx, a)
...
for _, fn := range a.opts.afterStop {
    err = fn(sctx)
}

To ensure that afterStopFn is not cancelled by the stop signal, you should change the context passed to afterStop functions from sctx (which is based on a.ctx) to a context based on o.ctx. Here is a conceptual modification to use o.ctx for afterStop functions:

// Right before executing afterStop functions
oCtxBased := NewContext(a.opts.ctx, a) // Using o.ctx from options to create a new context
for _, fn := range a.opts.afterStop {
    err = fn(oCtxBased) // Pass the new context based on o.ctx
}

This modification ensures that the afterStopFn functions will use a context (oCtxBased) that is not cancelled when the application's main context (a.ctx) is cancelled due to a stop signal. This allows the afterStopFn functions to complete their execution even after the application has begun its shutdown process.

As for the role of afterStopFn in the Kratos app lifecycle, it is executed after the application has been stopped. It is part of a series of hooks that the Kratos framework allows developers to use for executing custom logic at different stages of the application lifecycle. Specifically, afterStopFn functions are called after the application has gracefully stopped all its components, servers, and deregistered itself from the service registry if configured to do so.

I hope this helps! If you have any further questions, feel free to ask.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants