refactor: use signal.NotifyContext over custom implementation #10311

Merged
Contributor

Go 1.16 added the signal.NotifyContext helper utility. installSignals
could be further inlined in a future iteration, if needed.

When reading the function documentation, it becomes clear that this is doing the exact same thing as the old code.

Checklist

The contributor guide contains information that will be helpful to first time contributors. There also are a few conditions for merging Pull Requests in Forgejo repositories. You are also welcome to join the Forgejo development chatroom.

Tests

  • I added test coverage for Go changes...
    • in their respective *_test.go for unit tests.
    • in the tests/integration directory if it involves interactions with a live Forgejo server.
  • I added test coverage for JavaScript changes...

Documentation

  • I created a pull request to the documentation to explain to Forgejo users how to use this change.
  • I did not document these changes and I do not expect someone else to do it.

Release notes

  • I do not want this change to show in the release notes.
  • I want the title to show in the release notes with a link to this pull request.
  • I want the content of the release-notes/<pull request number>.md to be be used for the release notes instead of the title.
Go 1.16 added the signal.NotifyContext helper utility. `installSignals` could be further inlined in a future iteration, if needed. When reading the [function documentation](https://pkg.go.dev/os/signal#NotifyContext), it becomes clear that this is doing the exact same thing as the old code. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.
nachtjasmin force-pushed refactor-use-signal-notifycontext-over-custom-implementation from e78c341ef8
Some checks failed
requirements / merge-conditions (pull_request) Failing after 2s
issue-labels / backporting (pull_request_target) Has been skipped
issue-labels / cascade (pull_request_target) Has been skipped
issue-labels / release-notes (pull_request_target) Has been skipped
testing / frontend-checks (pull_request) Successful in 1m0s
testing / backend-checks (pull_request) Has been cancelled
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-remote-cacher (redict) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / security-check (pull_request) Has been cancelled
to 3cd541d1f6
Some checks failed
requirements / merge-conditions (pull_request) Failing after 2s
issue-labels / release-notes (pull_request_target) Has been skipped
testing / frontend-checks (pull_request) Successful in 58s
testing / backend-checks (pull_request) Successful in 2m54s
testing / test-unit (pull_request) Successful in 8m22s
testing / test-e2e (pull_request) Successful in 24m42s
testing / test-mysql (pull_request) Successful in 28m20s
testing / test-pgsql (pull_request) Successful in 37m36s
testing / test-remote-cacher (redis) (pull_request) Successful in 2m8s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m14s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m14s
testing / test-remote-cacher (redict) (pull_request) Successful in 2m16s
testing / test-sqlite (pull_request) Failing after 2h1m10s
testing / security-check (pull_request) Has been skipped
2025-12-03 22:41:10 +01:00
Compare
cmd/cmd.go Outdated
@ -109,3 +95,1 @@
}()
return ctx, cancel
return signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
Member

Could you please add an integration test to ensure this is capturing the same expected behavior?

It looks like you are correct, but a test would help prevent any regressions / unexpected corner-cases.

Could you please add an integration test to ensure this is capturing the same expected behavior? It looks like you are correct, but a test would help prevent any regressions / unexpected corner-cases.
Owner

Looking at how testing is implemented in the Go stdlib, this does not look fun to write any testing for. https://cs.opensource.google/go/go/+/master:src/os/signal/signal_test.go;l=711-777

Looking at how testing is implemented in the Go stdlib, this does not look fun to write any testing for. https://cs.opensource.google/go/go/+/master:src/os/signal/signal_test.go;l=711-777
Owner

I'm wondering if this is easier to be tested via E2E? CC @earl-warren

I'm wondering if this is easier to be tested via E2E? CC @earl-warren
Contributor

It is not fun indeed 😁 But not too difficult either. This can be a source of inspiration:

https://code.forgejo.org/forgejo/runner/src/branch/main/act/artifactcache/handler_test.go#L1175-L1208

It is not fun indeed 😁 But not too difficult either. This can be a source of inspiration: https://code.forgejo.org/forgejo/runner/src/branch/main/act/artifactcache/handler_test.go#L1175-L1208
Author
Contributor

Test added! Thanks for the link to the runner, that helped when finding a suitable test approach!

Test added! Thanks for the link to the runner, that helped when finding a suitable test approach!
Gusted marked this conversation as resolved
nachtjasmin force-pushed refactor-use-signal-notifycontext-over-custom-implementation from 3cd541d1f6
Some checks failed
requirements / merge-conditions (pull_request) Failing after 2s
issue-labels / release-notes (pull_request_target) Has been skipped
testing / frontend-checks (pull_request) Successful in 58s
testing / backend-checks (pull_request) Successful in 2m54s
testing / test-unit (pull_request) Successful in 8m22s
testing / test-e2e (pull_request) Successful in 24m42s
testing / test-mysql (pull_request) Successful in 28m20s
testing / test-pgsql (pull_request) Successful in 37m36s
testing / test-remote-cacher (redis) (pull_request) Successful in 2m8s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m14s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m14s
testing / test-remote-cacher (redict) (pull_request) Successful in 2m16s
testing / test-sqlite (pull_request) Failing after 2h1m10s
testing / security-check (pull_request) Has been skipped
to a4f586170b
Some checks failed
requirements / merge-conditions (pull_request) Failing after 3s
issue-labels / release-notes (pull_request_target) Has been skipped
testing / frontend-checks (pull_request) Successful in 59s
testing / backend-checks (pull_request) Has been cancelled
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-remote-cacher (redict) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / security-check (pull_request) Has been cancelled
2025-12-07 14:17:54 +01:00
Compare
nachtjasmin force-pushed refactor-use-signal-notifycontext-over-custom-implementation from a4f586170b
Some checks failed
requirements / merge-conditions (pull_request) Failing after 3s
issue-labels / release-notes (pull_request_target) Has been skipped
testing / frontend-checks (pull_request) Successful in 59s
testing / backend-checks (pull_request) Has been cancelled
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-remote-cacher (redict) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / security-check (pull_request) Has been cancelled
to 3dfb9209c4
All checks were successful
testing / frontend-checks (pull_request) Successful in 55s
testing / backend-checks (pull_request) Successful in 2m40s
testing / test-unit (pull_request) Successful in 6m27s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m31s
testing / test-remote-cacher (redis) (pull_request) Successful in 2m32s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m35s
testing / test-remote-cacher (redict) (pull_request) Successful in 2m35s
testing / test-e2e (pull_request) Successful in 17m50s
testing / test-mysql (pull_request) Successful in 22m39s
testing / test-sqlite (pull_request) Successful in 31m0s
testing / test-pgsql (pull_request) Successful in 34m11s
testing / security-check (pull_request) Successful in 1m40s
requirements / merge-conditions (pull_request) Successful in 2s
issue-labels / backporting (pull_request_target) Has been skipped
issue-labels / cascade (pull_request_target) Has been skipped
issue-labels / release-notes (pull_request_target) Has been skipped
2025-12-07 14:19:55 +01:00
Compare
cmd/cmd_test.go Outdated
@ -0,0 +1,37 @@
package cmd
Owner

Missing copyright headers

Missing copyright headers
Author
Contributor

Added and adjusted for 2026.

Added and adjusted for 2026.
nachtjasmin marked this conversation as resolved
cmd/cmd_test.go Outdated
@ -0,0 +30,4 @@
case <-time.Tick(time.Second * 10):
t.Fatalf("Context not cancelled via signal after 10 seconds")
case <-ctx.Done():
t.Logf("Context was cancelled")
Owner

I expected to see something like assert.True(t, true). Currently this test always pass right?

I expected to see something like `assert.True(t, true)`. Currently this test always pass right?
Author
Contributor

It doesn't. installSignals is modifying the context we're testing. If you either remove one of the signals in the installSignals method or add a new one to test for, the test actually fails.

It doesn't. `installSignals` is modifying the context we're testing. If you either remove one of the signals in the `installSignals` method or add a new one to test for, the test actually fails.
Gusted marked this conversation as resolved
nachtjasmin force-pushed refactor-use-signal-notifycontext-over-custom-implementation from 3dfb9209c4
All checks were successful
testing / frontend-checks (pull_request) Successful in 55s
testing / backend-checks (pull_request) Successful in 2m40s
testing / test-unit (pull_request) Successful in 6m27s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m31s
testing / test-remote-cacher (redis) (pull_request) Successful in 2m32s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m35s
testing / test-remote-cacher (redict) (pull_request) Successful in 2m35s
testing / test-e2e (pull_request) Successful in 17m50s
testing / test-mysql (pull_request) Successful in 22m39s
testing / test-sqlite (pull_request) Successful in 31m0s
testing / test-pgsql (pull_request) Successful in 34m11s
testing / security-check (pull_request) Successful in 1m40s
requirements / merge-conditions (pull_request) Successful in 2s
issue-labels / backporting (pull_request_target) Has been skipped
issue-labels / cascade (pull_request_target) Has been skipped
issue-labels / release-notes (pull_request_target) Has been skipped
to 4ceb145dbb
All checks were successful
issue-labels / release-notes (pull_request_target) Has been skipped
requirements / merge-conditions (pull_request) Successful in 2s
testing / semgrep/ci (pull_request) Successful in 14s
testing / frontend-checks (pull_request) Successful in 1m15s
testing / backend-checks (pull_request) Successful in 4m15s
testing / test-unit (pull_request) Successful in 7m1s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m11s
testing / test-remote-cacher (redis) (pull_request) Successful in 2m11s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m45s
testing / test-remote-cacher (redict) (pull_request) Successful in 2m46s
testing / test-e2e (pull_request) Successful in 24m27s
testing / test-mysql (pull_request) Successful in 25m51s
testing / test-sqlite (pull_request) Successful in 30m46s
testing / test-pgsql (pull_request) Successful in 34m1s
testing / security-check (pull_request) Successful in 55s
issue-labels / backporting (pull_request_target) Has been skipped
milestone / set (pull_request_target) Successful in 13s
2026-02-26 22:18:53 +01:00
Compare
Gusted approved these changes 2026-03-04 00:39:50 +01:00
Gusted left a comment
Owner

Thank you!

Thank you!
Gusted merged commit b21193ee50 into forgejo 2026-03-04 00:45:40 +01:00
Sign in to join this conversation.
No reviewers
No labels
arch
riscv64
backport/v1.19
backport/v1.20
backport/v1.21/forgejo
backport/v10.0/forgejo
backport/v11.0/forgejo
backport/v12.0/forgejo
backport/v13.0/forgejo
backport/v14.0/forgejo
backport/v7.0/forgejo
backport/v8.0/forgejo
backport/v9.0/forgejo
breaking
bug
bug
confirmed
bug
duplicate
bug
needs-more-info
bug
new-report
bug
reported-upstream
code/actions
code/api
code/auth
code/auth/faidp
code/auth/farp
code/email
code/federation
code/git
code/migrations
code/packages
code/wiki
database
MySQL
database
PostgreSQL
database
SQLite
dependency-upgrade
dependency
certmagic
dependency
chart.js
dependency
Chi
dependency
Chroma
dependency
citation.js
dependency
codespell
dependency
css-loader
dependency
devcontainers
dependency
dropzone
dependency
editorconfig-checker
dependency
elasticsearch
dependency
enmime
dependency
F3
dependency
ForgeFed
dependency
garage
dependency
Git
dependency
git-backporting
dependency
Gitea
dependency
gitignore
dependency
go-ap
dependency
go-enry
dependency
go-gitlab
dependency
Go-org
dependency
go-rpmutils
dependency
go-sql-driver mysql
dependency
go-swagger
dependency
go-version
dependency
go-webauthn
dependency
gocron
dependency
Golang
dependency
goldmark
dependency
goquery
dependency
Goth
dependency
grpc-go
dependency
happy-dom
dependency
Helm
dependency
image-spec
dependency
jsonschema
dependency
KaTeX
dependency
lint
dependency
MariaDB
dependency
Mermaid
dependency
minio-go
dependency
misspell
dependency
Monaco
dependency
PDFobject
dependency
playwright
dependency
postcss
dependency
postcss-plugins
dependency
pprof
dependency
prometheus client_golang
dependency
protobuf
dependency
relative-time-element
dependency
renovate
dependency
reply
dependency
ssh
dependency
swagger-ui
dependency
tailwind
dependency
temporal-polyfill
dependency
terminal-to-html
dependency
tests-only
dependency
text-expander-element
dependency
urfave
dependency
vfsgen
dependency
vite
dependency
Woodpecker CI
dependency
x tools
dependency
XORM
Discussion
duplicate
enhancement/feature
forgejo/accessibility
forgejo/branding
forgejo/ci
forgejo/commit-graph
forgejo/documentation
forgejo/furnace cleanup
forgejo/i18n
forgejo/interop
forgejo/moderation
forgejo/privacy
forgejo/release
forgejo/scaling
forgejo/security
forgejo/ui
Gain
High
Gain
Nice to have
Gain
Undefined
Gain
Very High
good first issue
i18n/backport-stable
impact
large
impact
medium
impact
small
impact
unknown
Incompatible license
issue
closed
issue
do-not-exist-yet
issue
open
manual test
Manually tested during feature freeze
OS
FreeBSD
OS
Linux
OS
macOS
OS
Windows
problem
QA
regression
release blocker
Release Cycle
Feature Freeze
release-blocker
v7.0
release-blocker
v7.0.1
release-blocker
v7.0.2
release-blocker
v7.0.3
release-blocker
v7.0.4
release-blocker
v8.0.0
release-blocker/v9.0.0
run-all-playwright-tests
run-end-to-end-tests
test
manual
test
needed
test
needs-help
test
not-needed
test
present
untested
User research - time-tracker
valuable code
worth a release-note
User research - Accessibility
User research - Blocked
User research - Community
User research - Config (instance)
User research - Errors
User research - Filters
User research - Future backlog
User research - Git workflow
User research - Labels
User research - Moderation
User research - Needs input
User research - Notifications/Dashboard
User research - Rendering
User research - Repo creation
User research - Repo units
User research - Security
User research - Settings (in-app)
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
forgejo/forgejo!10311
No description provided.