diff --git a/internal/protocol/gopher/gopher.go b/internal/protocol/gopher/gopher.go index e79f5c4..7891bf0 100644 --- a/internal/protocol/gopher/gopher.go +++ b/internal/protocol/gopher/gopher.go @@ -231,6 +231,15 @@ func (p *Protocol) downloadText(ctx context.Context, reader *bufio.Reader, req * line, err := reader.ReadString('\n') if err != nil { if err == io.EOF { + // The connection may have been closed by our context + // cancellation goroutine (or by the server after the + // client cancelled). If so, the EOF is not a genuine + // end-of-data but a side-effect of the cancel and we + // must persist the partial state before bailing. + if ctx.Err() != nil { + saveGopherResume(req, bytesWritten) + return nil, ctx.Err() + } break } saveGopherResume(req, bytesWritten)