Bounty
XE-13279 · Bounty

[net/msg.go] L8: request() tidak handle context cancellation sebelum Invoke selesai — resource leak

Closed · invalid — xe#424 filed 2026-11-12 02:49 UTC by wahyu20260100233-ops

Report

Ringkasan

Context cancellation hanya di-handle di select setelah Invoke selesai. Kalau context cancelled sebelum Invoke selesai, goroutine tetap jalan sampai Invoke return.

Lokasi

  • File: net/msg.go
  • Lines: 130-143

Kode

func (m *Messenger) request(pid peer.ID, msgType string, payload interface{}, timeout time.Duration) (json.RawMessage, error) {
    ...
    select {
    case resp := <-respCh:
        return resp.payload, resp.err
    case <-ctx.Done():
        return nil, ctx.Err()
    case <-time.After(timeout):
        return nil, ErrTimeout
    }
}

Impact

Kalau context cancelled, goroutine yang menjalankan handler tetap jalan sampai selesai. Resource leak (goroutine, memory) untuk handler yang lambat.

Suggested Fix

func (m *Messenger) request(pid peer.ID, msgType string, payload interface{}, timeout time.Duration) (json.RawMessage, error) {
    ...
    ctx, cancel := context.WithTimeout(context.Background(), timeout)
    defer cancel()
    
    respCh := make(chan msgResponse, 1)
    go func() {
        select {
        case respCh <- m.executeHandler(pid, msgType, payload):
        case <-ctx.Done():
            // handler mungkin masih jalan, tapi kita cancel context
        }
    }()
    
    select {
    case resp := <-respCh:
        return resp.payload, resp.err
    case <-ctx.Done():
        return nil, ctx.Err()
    }
}

Atau implement cancel mechanism di dalam handler execution.

go func() {
    defer func() {
        if ctx.Err() != nil {
            // batalkan handler execution
        }
    }()
    respCh <- m.executeHandler(pid, msgType, payload)
}()

Thread

XE deskteam2026-11-20 00:49 UTCgithub

Thanks for the report. We read it against the code and could not confirm a bug in what it describes.

This report is closed as invalid. If we have missed something, file a new report that links https://bounty.xe.network/XE-13279 and says what; it is read like any other.