The HTTP endpoint handler for invoice synchronization in a Go 1.24 service (standard library) needs to be fixed. The server runs in the normal go test ./... mode without additional dependencies. The handler accepts POST /invoices/sync with JSON {"invoice_id":"...","customer_id":"..."} and must: 1) return 200 OK only after the synchronization result has been saved; 2) not lose save errors; 3) not continue background work after the request is canceled; 4) close the request body. In the current implementation, behavior is unstable: sometimes the response arrives before the write, sometimes request cancellation is ignored, and repeated calls with the same invoice may duplicate the result. It is assumed there is one process, one HTTP server, and synchronization between goroutines must be explicit and without unsafely accessing shared data.
Go 1.24; standard library only; tests run with go test ./... on Linux amd64; goroutine synchronization assumptions: any shared structures must be protected by a mutex or passed through channels, you cannot rely on scheduler order; HTTP request bodies must be closed by the handler when it finishes reading them.
Use line comments to explain the problem, its impact, the fix and how to test it.