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

Fix Go runtime shutdown function ctx #1231

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com) and this project uses [semantic versioning](http://semver.org).

## [Unreleased]
### Fixed
- Correctly wire Go runtime shutdown function context.

## [3.22.0] - 2024-06-09
### Added
Expand Down
2 changes: 1 addition & 1 deletion server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var (
}
metrics = NewLocalMetrics(logger, logger, nil, cfg)
storageIdx, _ = NewLocalStorageIndex(logger, nil, &StorageConfig{DisableIndexOnly: false}, metrics)
_ = cfg.Validate(logger)
_ = ValidateConfig(logger, cfg)
)

type DummyMessageRouter struct{}
Expand Down
1 change: 1 addition & 0 deletions server/runtime_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -2674,6 +2674,7 @@ func (ri *RuntimeGoInitializer) RegisterFleetManager(fleetManager runtime.FleetM

func (ri *RuntimeGoInitializer) RegisterShutdown(fn func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule)) error {
ri.shutdownFunction = func(ctx context.Context) {
ctx = NewRuntimeGoContext(ctx, ri.node, ri.version, ri.env, RuntimeExecutionModeShutdown, nil, nil, 0, "", "", nil, "", "", "", "")
fn(ctx, ri.logger.WithField("mode", RuntimeExecutionModeShutdown.String()), ri.db, ri.nk)
}

Expand Down
11 changes: 7 additions & 4 deletions server/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"errors"
"go.uber.org/zap"
"os"
"time"
Expand All @@ -14,18 +15,20 @@ func HandleShutdown(ctx context.Context, logger *zap.Logger, matchRegistry Match
runtimeShutdownFnDone := make(chan struct{}, 1)

if graceSeconds != 0 {
timer = time.NewTimer(time.Duration(graceSeconds) * time.Second)
timerCh = timer.C

graceDuration := time.Duration(graceSeconds) * time.Second
if shutdownFn != nil {
go func() {
shutdownFn(ctx)
shCtx, _ := context.WithTimeoutCause(context.WithoutCancel(ctx), graceDuration, errors.New("grace period expired"))
sesposito marked this conversation as resolved.
Show resolved Hide resolved
shutdownFn(shCtx)
close(runtimeShutdownFnDone)
}()
} else {
close(runtimeShutdownFnDone)
}

timer = time.NewTimer(graceDuration)
timerCh = timer.C

logger.Info("Shutdown started - use CTRL^C to force stop server", zap.Int("grace_period_sec", graceSeconds))
} else {
// No grace period.
Expand Down
Loading