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: Resolve race condition in HTTP server handling #20

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 16 additions & 15 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,31 +169,32 @@ func formatRunes(s string) string {
//
// Here's an example that you can play with to better understand the behaviour:
//
// err = diff.TestdataJSON(filepath.Join("testdata", t.Name()), "change me")
// if err != nil {
// t.Fatal(err)
// }
// err = diff.TestdataJSON(filepath.Join("testdata", t.Name()), "change me")
// if err != nil {
// t.Fatal(err)
// }
//
// Normally you want to use t.Name() as path for clarity but you can pass in any string.
// e.g. a single test could persist two json objects into testdata with:
//
// err = diff.TestdataJSON(filepath.Join("testdata", t.Name(), "1"), "change me 1")
// if err != nil {
// t.Fatal(err)
// }
// err = diff.TestdataJSON(filepath.Join("testdata", t.Name(), "2"), "change me 2")
// if err != nil {
// t.Fatal(err)
// }
// err = diff.TestdataJSON(filepath.Join("testdata", t.Name(), "1"), "change me 1")
// if err != nil {
// t.Fatal(err)
// }
// err = diff.TestdataJSON(filepath.Join("testdata", t.Name(), "2"), "change me 2")
// if err != nil {
// t.Fatal(err)
// }
//
// These would persist in testdata/${t.Name()}/1.exp.json and testdata/${t.Name()}/2.exp.json
//
// It uses Files under the hood.
//
// note: testdata is the canonical Go directory for such persistent test only files.
// It is unfortunately poorly documented. See https://pkg.go.dev/cmd/go/internal/test
// So normally you'd want path to be filepath.Join("testdata", t.Name()).
// This is also the reason this function is named "TestdataJSON".
//
// It is unfortunately poorly documented. See https://pkg.go.dev/cmd/go/internal/test
// So normally you'd want path to be filepath.Join("testdata", t.Name()).
// This is also the reason this function is named "TestdataJSON".
func TestdataJSON(path string, got interface{}) error {
gotb := xjson.Marshal(got)
gotb = append(gotb, '\n')
Expand Down
45 changes: 43 additions & 2 deletions xhttp/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package xhttp

import (
"context"
"errors"
"log"
"net"
"net/http"
"sync"
"sync/atomic"
"time"

"oss.terrastruct.com/util-go/xcontext"
Expand All @@ -22,15 +25,52 @@ func NewServer(log *log.Logger, h http.Handler) *http.Server {
}
}

type safeServer struct {
*http.Server
running int32
mu sync.Mutex
}

func newSafeServer(s *http.Server) *safeServer {
return &safeServer{
Server: s,
}
}

func (s *safeServer) ListenAndServe(l net.Listener) error {
s.mu.Lock()
defer s.mu.Unlock()

if !atomic.CompareAndSwapInt32(&s.running, 0, 1) {
return errors.New("server is already running")
}
defer atomic.StoreInt32(&s.running, 0)

return s.Serve(l)
}

func (s *safeServer) Shutdown(ctx context.Context) error {
s.mu.Lock()
defer s.mu.Unlock()

if atomic.LoadInt32(&s.running) == 0 {
return nil
}

return s.Server.Shutdown(ctx)
}

func Serve(ctx context.Context, shutdownTimeout time.Duration, s *http.Server, l net.Listener) error {
s.BaseContext = func(net.Listener) context.Context {
return ctx
}

ss := newSafeServer(s)

serverClosed := make(chan struct{})
var serverError error
go func() {
serverError = s.Serve(l)
serverError = ss.ListenAndServe(l)
close(serverClosed)
}()

Expand All @@ -41,11 +81,12 @@ func Serve(ctx context.Context, shutdownTimeout time.Duration, s *http.Server, l
shutdownCtx, cancel := context.WithTimeout(xcontext.WithoutCancel(ctx), shutdownTimeout)
defer cancel()

err := s.Shutdown(shutdownCtx)
err := ss.Shutdown(shutdownCtx)
<-serverClosed // Wait for server to exit
if err != nil {
return err
}
return serverError
}

}
Loading