Skip to content

Commit

Permalink
Clean up certmagic locks on signaled process exit
Browse files Browse the repository at this point in the history
This should help prevent hanging in some cases when the process is
restarted and tries to obtain or renew a certificate, for example, but
the lock remains from the previous shutdown (which was during the same
operation). Only works if the process is cleanly shut down with a signal
it can capture.
  • Loading branch information
mholt committed Jun 19, 2019
1 parent 0c626fb commit 6720bdf
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/klauspost/cpuid v1.2.0
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 // indirect
github.com/lucas-clemente/quic-go v0.10.2
github.com/mholt/certmagic v0.5.0
github.com/mholt/certmagic v0.6.1-0.20190619225040-c7a9fdb17816
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/naoina/toml v0.1.1
github.com/russross/blackfriday v0.0.0-20170610170232-067529f716f4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/lucas-clemente/quic-go v0.10.2/go.mod h1:hvaRS9IHjFLMq76puFJeWNfmn+H7
github.com/lucas-clemente/quic-go-certificates v0.0.0-20160823095156-d2f86524cced h1:zqEC1GJZFbGZA0tRyNZqRjep92K5fujFtFsu5ZW7Aug=
github.com/lucas-clemente/quic-go-certificates v0.0.0-20160823095156-d2f86524cced/go.mod h1:NCcRLrOTZbzhZvixZLlERbJtDtYsmMw8Jc4vS8Z0g58=
github.com/marten-seemann/qtls v0.2.3/go.mod h1:xzjG7avBwGGbdZ8dTGxlBnLArsVKLvwmjgmPuiQEcYk=
github.com/mholt/certmagic v0.5.0 h1:lYXxsLUFya/I3BgDCrfuwcMQOB+4auzI8CCzpK41tjc=
github.com/mholt/certmagic v0.5.0/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9VvG35NY=
github.com/mholt/certmagic v0.6.1-0.20190619225040-c7a9fdb17816 h1:DEoTKH/n8k9JXYqM/A3AmW407Q5Rncrkuipqu0mztcg=
github.com/mholt/certmagic v0.6.1-0.20190619225040-c7a9fdb17816/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9VvG35NY=
github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM=
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks=
Expand Down
3 changes: 3 additions & 0 deletions sigtrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sync"

"github.com/mholt/caddy/telemetry"
"github.com/mholt/certmagic"
)

// TrapSignals create signal handlers for all applicable signals for this
Expand Down Expand Up @@ -49,6 +50,7 @@ func trapSignalsCrossPlatform() {
for _, f := range OnProcessExit {
f() // important cleanup actions only
}
certmagic.CleanUpOwnLocks()
os.Exit(2)
}

Expand All @@ -63,6 +65,7 @@ func trapSignalsCrossPlatform() {
}

go func() {
certmagic.CleanUpOwnLocks()
os.Exit(executeShutdownCallbacks("SIGINT"))
}()
}
Expand Down
3 changes: 3 additions & 0 deletions sigtrap_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"syscall"

"github.com/mholt/caddy/telemetry"
"github.com/mholt/certmagic"
)

// trapSignalsPosix captures POSIX-only signals.
Expand All @@ -38,6 +39,7 @@ func trapSignalsPosix() {
for _, f := range OnProcessExit {
f() // only perform important cleanup actions
}
certmagic.CleanUpOwnLocks()
os.Exit(0)

case syscall.SIGTERM:
Expand All @@ -55,6 +57,7 @@ func trapSignalsPosix() {
telemetry.AppendUnique("sigtrap", "SIGTERM")
go telemetry.StopEmitting() // won't finish in time, but that's OK - just don't block

certmagic.CleanUpOwnLocks()
os.Exit(exitCode)

case syscall.SIGUSR1:
Expand Down

0 comments on commit 6720bdf

Please sign in to comment.