diff --git a/client.go b/client.go index de417ac..b434bbf 100644 --- a/client.go +++ b/client.go @@ -89,6 +89,16 @@ func (c *client) ReleaseID(reqID uint16) { // writeRequest writes params and stdin to the FastCGI application func (c *client) writeRequest(req *Request) (err error) { + // end request whenever the function block ends + defer func() { + if err != nil { + // abort the request if there is any error + // in previous request writing process. + c.conn.writeAbortRequest(req.ID) + return + } + }() + // write request header with specified role err = c.conn.writeBeginRequest(req.ID, req.Role, 0) if err != nil { @@ -109,7 +119,7 @@ func (c *client) writeRequest(req *Request) (err error) { if err == io.EOF { err = nil } else if err != nil { - break + return } if count == 0 { break @@ -117,7 +127,7 @@ func (c *client) writeRequest(req *Request) (err error) { err = c.conn.writeRecord(typeStdin, req.ID, p[:count]) if err != nil { - break + return } } } @@ -132,7 +142,7 @@ func (c *client) writeRequest(req *Request) (err error) { if err == io.EOF { err = nil } else if err != nil { - break + return } if count == 0 { break @@ -140,7 +150,7 @@ func (c *client) writeRequest(req *Request) (err error) { err = c.conn.writeRecord(typeData, req.ID, p[:count]) if err != nil { - break + return } } } diff --git a/fcgi.go b/fcgi.go index 1ac230f..265fefb 100644 --- a/fcgi.go +++ b/fcgi.go @@ -170,6 +170,10 @@ func (c *conn) writeEndRequest(reqID uint16, appStatus int, protocolStatus uint8 return c.writeRecord(typeEndRequest, reqID, b) } +func (c *conn) writeAbortRequest(reqID uint16) error { + return c.writeRecord(typeAbortRequest, reqID, nil) +} + func (c *conn) writePairs(recType recType, reqID uint16, pairs map[string]string) error { w := newWriter(c, recType, reqID) b := make([]byte, 8)