Skip to content
Merged
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
18 changes: 16 additions & 2 deletions internal/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ type AsyncTransport struct {

flushRequest chan chan struct{}

closeMu sync.RWMutex

QueueSize int
Timeout time.Duration

Expand Down Expand Up @@ -392,6 +394,9 @@ func (t *AsyncTransport) HasCapacity() bool {
}

func (t *AsyncTransport) SendEnvelope(envelope *protocol.Envelope) error {
t.closeMu.RLock()
defer t.closeMu.RUnlock()

select {
case <-t.done:
return ErrTransportClosed
Expand All @@ -411,6 +416,8 @@ func (t *AsyncTransport) SendEnvelope(envelope *protocol.Envelope) error {
identifier := util.EnvelopeIdentifier(envelope)

select {
case <-t.done:
return ErrTransportClosed
Comment thread
cursor[bot] marked this conversation as resolved.
case t.queue <- envelope:
debuglog.Printf(
"Sending %s to %s project: %s",
Expand All @@ -432,8 +439,14 @@ func (t *AsyncTransport) Flush(timeout time.Duration) bool {
}

func (t *AsyncTransport) FlushWithContext(ctx context.Context) bool {
t.closeMu.RLock()
defer t.closeMu.RUnlock()

flushResponse := make(chan struct{})
select {
case <-t.done:
debuglog.Println("Failed to flush, transport is closed.")
return false
case t.flushRequest <- flushResponse:
select {
case <-flushResponse:
Expand All @@ -451,9 +464,10 @@ func (t *AsyncTransport) FlushWithContext(ctx context.Context) bool {

func (t *AsyncTransport) Close() {
t.closeOnce.Do(func() {
t.closeMu.Lock()
defer t.closeMu.Unlock()

close(t.done)
close(t.queue)
close(t.flushRequest)
t.wg.Wait()
})
}
Expand Down
13 changes: 13 additions & 0 deletions internal/http/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,19 @@ func TestAsyncTransport_FlushWithContext(t *testing.T) {
t.Error("FlushWithContext should timeout")
}
})

t.Run("closed transport", func(t *testing.T) {
tr := NewAsyncTransport(TransportOptions{Dsn: "https://key@sentry.io/123"})
transport, ok := tr.(*AsyncTransport)
if !ok {
t.Fatalf("expected *AsyncTransport, got %T", tr)
}
transport.Close()

if transport.FlushWithContext(context.Background()) {
t.Error("FlushWithContext should fail for a closed transport")
}
})
}

func TestAsyncTransport_Close(t *testing.T) {
Expand Down
Loading