aboutsummaryrefslogtreecommitdiff
path: root/setup/base
diff options
context:
space:
mode:
authorTill <2353100+S7evinK@users.noreply.github.com>2022-08-30 13:59:13 +0200
committerGitHub <noreply@github.com>2022-08-30 13:59:13 +0200
commitb0e2ea0f374892a6f9c77623bc6c3fd9ea572b3b (patch)
treee73572ebe2b32d98cd411aa039bf85da6d802487 /setup/base
parentbbb3ade4a2b49cfdaf7ec86ddf079ff7d48e0cf3 (diff)
Fix race condition on startup (#2679)
`SetupAndServeHTTP` would race in `configureHTTPErrors` and while configuring routes.
Diffstat (limited to 'setup/base')
-rw-r--r--setup/base/base.go18
1 files changed, 13 insertions, 5 deletions
diff --git a/setup/base/base.go b/setup/base/base.go
index b21eeba4..87f41576 100644
--- a/setup/base/base.go
+++ b/setup/base/base.go
@@ -25,21 +25,23 @@ import (
_ "net/http/pprof"
"os"
"os/signal"
+ "sync"
"syscall"
"time"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
- "github.com/matrix-org/dendrite/internal/caching"
- "github.com/matrix-org/dendrite/internal/httputil"
- "github.com/matrix-org/dendrite/internal/pushgateway"
- "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/gomatrixserverlib"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/atomic"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
+ "github.com/matrix-org/dendrite/internal/caching"
+ "github.com/matrix-org/dendrite/internal/httputil"
+ "github.com/matrix-org/dendrite/internal/pushgateway"
+ "github.com/matrix-org/dendrite/internal/sqlutil"
+
"github.com/matrix-org/dendrite/internal"
"github.com/matrix-org/dendrite/setup/jetstream"
"github.com/matrix-org/dendrite/setup/process"
@@ -47,6 +49,8 @@ import (
"github.com/gorilla/mux"
"github.com/kardianos/minwinsvc"
+ "github.com/sirupsen/logrus"
+
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
asinthttp "github.com/matrix-org/dendrite/appservice/inthttp"
federationAPI "github.com/matrix-org/dendrite/federationapi/api"
@@ -58,7 +62,6 @@ import (
"github.com/matrix-org/dendrite/setup/config"
userapi "github.com/matrix-org/dendrite/userapi/api"
userapiinthttp "github.com/matrix-org/dendrite/userapi/inthttp"
- "github.com/sirupsen/logrus"
)
// BaseDendrite is a base for creating new instances of dendrite. It parses
@@ -87,6 +90,7 @@ type BaseDendrite struct {
Database *sql.DB
DatabaseWriter sqlutil.Writer
EnableMetrics bool
+ startupLock sync.Mutex
}
const NoListener = ""
@@ -394,6 +398,9 @@ func (b *BaseDendrite) SetupAndServeHTTP(
internalHTTPAddr, externalHTTPAddr config.HTTPAddress,
certFile, keyFile *string,
) {
+ // Manually unlocked right before actually serving requests,
+ // as we don't return from this method (defer doesn't work).
+ b.startupLock.Lock()
internalAddr, _ := internalHTTPAddr.Address()
externalAddr, _ := externalHTTPAddr.Address()
@@ -472,6 +479,7 @@ func (b *BaseDendrite) SetupAndServeHTTP(
externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.PublicMediaAPIMux)
externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(b.PublicWellKnownAPIMux)
+ b.startupLock.Unlock()
if internalAddr != NoListener && internalAddr != externalAddr {
go func() {
var internalShutdown atomic.Bool // RegisterOnShutdown can be called more than once