diff options
| author | srdusr <[email protected]> | 2024-09-19 23:57:00 +0200 |
|---|---|---|
| committer | srdusr <[email protected]> | 2024-09-19 23:57:00 +0200 |
| commit | aae93b4575e10d223c6cdd8722ca0cce2d47397c (patch) | |
| tree | 73b75f132de70c5a6298c0b2c5de1c1e28ea0d34 /internal/ipc | |
| parent | a02d612ba5efb5f1e79a2f9a0778890e8c3e6acf (diff) | |
| download | mitmux-aae93b4575e10d223c6cdd8722ca0cce2d47397c.tar.gz mitmux-aae93b4575e10d223c6cdd8722ca0cce2d47397c.zip | |
Fix hang and data-race bugs found while re-verifying steps 1-5
Audited every file in the proxy/store/ipc/TUI stack before starting
step 6, per request. Found and fixed three real bugs in already-shipped
code, all confirmed with live tests (including a race-detector build)
rather than just read:
1. No timeout covered the write-request/read-response phase of an
upstream exchange, in either the main proxy path (roundTripH1/
roundTripH2) or Repeater - only the dial itself was bounded. A
server that accepted the connection and then never finished
responding hung the request forever. Fixed with conn.SetDeadline
after a successful dial in both forward() and Repeat() (new
upstreamTimeout constant, 60s). Verified against a real hung TCP
listener: the daemon returned a clean "i/o timeout" error at exactly
60s instead of hanging.
2. ipc.Client shared one connection/encoder/decoder with no locking.
Bubble Tea dispatches each request as its own goroutine, and
viewList's 'r' key doesn't change mode while its loadDetail call is
in flight - pressing it again (or 'enter' on another row) before the
first response arrives calls Get/List/Repeat concurrently on the same
connection, which can interleave JSON on the wire or hand one call
another's response. Fixed with a mutex serializing round trips.
Stress-tested with rapid overlapping key input against a -race build
of both binaries: no warnings, no corruption.
3. The IPC "subscribe" handler only noticed a disconnected client when
the next broadcast's Encode failed - a subscriber that quit while the
daemon was otherwise idle leaked its goroutine and channel
indefinitely. Fixed by reading the connection in the background too,
so disconnection is detected immediately regardless of traffic.
Also removed a dead, misleading parameter: captureResponse took a *teeConn
it was never actually called with (the exact-capture path is handled
directly in forward()), so the branch using it was unreachable.
Re-verified all five prior steps end-to-end against a fresh build:
plain HTTP, HTTPS H1.1/H2/untrusted-CA-rejection, exact vs reconstructed
capture flags cross-checked directly in SQLite, Repeater over both HTTP
and HTTPS, and search (plain text, dotted domains, hyphenated terms,
column filters) - all correct.
Diffstat (limited to 'internal/ipc')
| -rw-r--r-- | internal/ipc/ipc.go | 15 | ||||
| -rw-r--r-- | internal/ipc/server.go | 27 |
2 files changed, 38 insertions, 4 deletions
diff --git a/internal/ipc/ipc.go b/internal/ipc/ipc.go index 691dadf..f890715 100644 --- a/internal/ipc/ipc.go +++ b/internal/ipc/ipc.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "net" + "sync" "mitmux/internal/store" ) @@ -51,7 +52,15 @@ type EntryDetail struct { // Client talks to a mitmuxd instance for request/response queries // (list, get). Use Subscribe separately for the live-update stream. +// +// One request/response round trip is in flight on the connection at a +// time, guarded by mu - a caller like the mitmux TUI dispatches each +// request as its own goroutine (a Bubble Tea tea.Cmd), and without this +// two overlapping calls (e.g. opening two entries in quick succession) +// would interleave their JSON on the wire or hand one call the other's +// response. type Client struct { + mu sync.Mutex conn net.Conn dec *json.Decoder enc *json.Encoder @@ -84,6 +93,8 @@ func (c *Client) Search(query string, limit int, beforeID int64) ([]store.Summar } func (c *Client) list(req Request) ([]store.Summary, error) { + c.mu.Lock() + defer c.mu.Unlock() if err := c.enc.Encode(req); err != nil { return nil, err } @@ -99,6 +110,8 @@ func (c *Client) list(req Request) ([]store.Summary, error) { // Get returns the full entry (raw bytes included) for id. func (c *Client) Get(id int64) (*EntryDetail, error) { + c.mu.Lock() + defer c.mu.Unlock() if err := c.enc.Encode(Request{Type: "get", ID: id}); err != nil { return nil, err } @@ -116,6 +129,8 @@ func (c *Client) Get(id int64) (*EntryDetail, error) { // no header injection) and returns the resulting entry, including the raw // response bytes. The exchange is also recorded to history. func (c *Client) Repeat(scheme, host string, raw []byte) (*EntryDetail, error) { + c.mu.Lock() + defer c.mu.Unlock() if err := c.enc.Encode(Request{Type: "repeat", Scheme: scheme, Host: host, Raw: raw}); err != nil { return nil, err } diff --git a/internal/ipc/server.go b/internal/ipc/server.go index 0e839fb..69044af 100644 --- a/internal/ipc/server.go +++ b/internal/ipc/server.go @@ -3,6 +3,7 @@ package ipc import ( "context" "encoding/json" + "io" "log" "net" "sync" @@ -129,13 +130,31 @@ func (s *Server) handleConn(conn net.Conn) { case "subscribe": sub := s.hub.subscribe() defer s.hub.unsubscribe(sub) - for e := range sub { - e := e - if err := enc.Encode(Response{Type: "new", New: &e}); err != nil { + + // The client never sends anything more on this connection, + // but reading it anyway is how we notice it went away when + // no new entry ever arrives to trigger a failed Encode - + // otherwise a subscriber that quits while the daemon is + // idle leaks its goroutine and channel indefinitely. + disconnected := make(chan struct{}) + go func() { + io.Copy(io.Discard, conn) + close(disconnected) + }() + + for { + select { + case e, ok := <-sub: + if !ok { + return + } + if err := enc.Encode(Response{Type: "new", New: &e}); err != nil { + return + } + case <-disconnected: return } } - return default: enc.Encode(Response{Type: "error", Error: "unknown request type: " + req.Type}) |