diff --git a/consensus/replay.go b/consensus/replay.go index 4557a7d05..d3ea91885 100644 --- a/consensus/replay.go +++ b/consensus/replay.go @@ -117,6 +117,7 @@ func (cs *ConsensusState) catchupReplay(csHeight int) error { } else { defer func() { if err := gr.Close(); err != nil { + cs.Logger.Error("Error closing wal Search", "err", err) return } }() diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 992201cdf..9e4dbb843 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -492,6 +492,7 @@ func makeBlockchainFromWAL(wal WAL) ([]*types.Block, []*types.Commit, error) { } defer func() { if err := gr.Close(); err != nil { + wal.Logger.Error("Error closing wal Search", "err", err) return } }() diff --git a/consensus/wal.go b/consensus/wal.go index 3f85f7da6..1d2c74e34 100644 --- a/consensus/wal.go +++ b/consensus/wal.go @@ -174,7 +174,6 @@ func (wal *baseWAL) SearchForEndHeight(height uint64) (gr *auto.GroupReader, fou } } } - gr.Close() } diff --git a/mempool/mempool.go b/mempool/mempool.go index d1475f331..ef2fa4950 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -3,6 +3,7 @@ package mempool import ( "bytes" "container/list" + "fmt" "sync" "sync/atomic" "time" @@ -191,11 +192,11 @@ func (mem *Mempool) CheckTx(tx types.Tx, cb func(*abci.Response)) (err error) { // TODO: Notify administrators when WAL fails _, err := mem.wal.Write([]byte(tx)) if err != nil { - return err + mem.logger.Error(fmt.Sprintf("Error writing to WAL: %v", err)) } _, err = mem.wal.Write([]byte("\n")) if err != nil { - return err + mem.logger.Error(fmt.Sprintf("Error writing to WAL: %v", err)) } } // END WAL @@ -337,9 +338,9 @@ func (mem *Mempool) collectTxs(maxTxs int) types.Txs { // Update informs the mempool that the given txs were committed and can be discarded. // NOTE: this should be called *after* block is committed by consensus. // NOTE: unsafe; Lock/Unlock must be managed by caller -func (mem *Mempool) Update(height int, txs types.Txs) { +func (mem *Mempool) Update(height int, txs types.Txs) error { if err := mem.proxyAppConn.FlushSync(); err != nil { // To flush async resCb calls e.g. from CheckTx - panic(err) + return err } // First, create a lookup map of txns in new txs. txsMap := make(map[string]struct{}) @@ -363,6 +364,7 @@ func (mem *Mempool) Update(height int, txs types.Txs) { // mem.recheckCursor re-scans mem.txs and possibly removes some txs. // Before mem.Reap(), we should wait for mem.recheckCursor to be nil. } + return nil } func (mem *Mempool) filterTxs(blockTxsMap map[string]struct{}) []types.Tx { diff --git a/p2p/upnp/upnp.go b/p2p/upnp/upnp.go index a90b10044..43e943480 100644 --- a/p2p/upnp/upnp.go +++ b/p2p/upnp/upnp.go @@ -40,11 +40,7 @@ func Discover() (nat NAT, err error) { return } socket := conn.(*net.UDPConn) - defer func() { - if err := socket.Close(); err != nil { - return - } - }() + defer socket.Close() if err := socket.SetDeadline(time.Now().Add(3 * time.Second)); err != nil { return nil, err @@ -201,11 +197,8 @@ func getServiceURL(rootURL string) (url, urnDomain string, err error) { if err != nil { return } - defer func() { - if err := r.Body.Close(); err != nil { - return - } - }() + defer r.Body.Close() + if r.StatusCode >= 400 { err = errors.New(string(r.StatusCode)) return @@ -303,11 +296,7 @@ func (n *upnpNAT) getExternalIPAddress() (info statusInfo, err error) { var response *http.Response response, err = soapRequest(n.serviceURL, "GetExternalIPAddress", message, n.urnDomain) if response != nil { - defer func() { - if err := response.Body.Close(); err != nil { - return - } - }() + defer response.Body.Close() } if err != nil { return @@ -356,11 +345,7 @@ func (n *upnpNAT) AddPortMapping(protocol string, externalPort, internalPort int var response *http.Response response, err = soapRequest(n.serviceURL, "AddPortMapping", message, n.urnDomain) if response != nil { - defer func() { - if err := response.Body.Close(); err != nil { - return - } - }() + defer response.Body.Close() } if err != nil { return @@ -386,11 +371,7 @@ func (n *upnpNAT) DeletePortMapping(protocol string, externalPort, internalPort var response *http.Response response, err = soapRequest(n.serviceURL, "DeletePortMapping", message, n.urnDomain) if response != nil { - defer func() { - if err := response.Body.Close(); err != nil { - return - } - }() + defer response.Body.Close() } if err != nil { return diff --git a/rpc/lib/client/http_client.go b/rpc/lib/client/http_client.go index bfbae84c5..ea025818d 100644 --- a/rpc/lib/client/http_client.go +++ b/rpc/lib/client/http_client.go @@ -93,12 +93,8 @@ func (c *JSONRPCClient) Call(method string, params map[string]interface{}, resul if err != nil { return nil, err } - defer func() { - if err := httpResponse.Body.Close(); err != nil { - panic(err) - return - } - }() + defer httpResponse.Body.Close() + responseBytes, err := ioutil.ReadAll(httpResponse.Body) if err != nil { return nil, err @@ -133,12 +129,8 @@ func (c *URIClient) Call(method string, params map[string]interface{}, result in if err != nil { return nil, err } - defer func() { - if err := resp.Body.Close(); err != nil { - panic(err) - return - } - }() + defer resp.Body.Close() + responseBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err diff --git a/rpc/lib/client/ws_client.go b/rpc/lib/client/ws_client.go index 3bfcbf9fd..0b3ec2b8c 100644 --- a/rpc/lib/client/ws_client.go +++ b/rpc/lib/client/ws_client.go @@ -291,7 +291,7 @@ func (c *WSClient) processBacklog() error { case request := <-c.backlog: if c.writeWait > 0 { if err := c.conn.SetWriteDeadline(time.Now().Add(c.writeWait)); err != nil { - panic(err) + c.Logger.Error("failed to set write deadline", "err", err) } } if err := c.conn.WriteJSON(request); err != nil { @@ -363,7 +363,7 @@ func (c *WSClient) writeRoutine() { case request := <-c.send: if c.writeWait > 0 { if err := c.conn.SetWriteDeadline(time.Now().Add(c.writeWait)); err != nil { - panic(err) + c.Logger.Error("failed to set write deadline", "err", err) } } if err := c.conn.WriteJSON(request); err != nil { @@ -376,7 +376,7 @@ func (c *WSClient) writeRoutine() { case <-ticker.C: if c.writeWait > 0 { if err := c.conn.SetWriteDeadline(time.Now().Add(c.writeWait)); err != nil { - panic(err) + c.Logger.Error("failed to set write deadline", "err", err) } } if err := c.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil { @@ -392,7 +392,7 @@ func (c *WSClient) writeRoutine() { return case <-c.Quit: if err := c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")); err != nil { - panic(err) + c.Logger.Error("failed to write message", "err", err) } return } @@ -424,7 +424,7 @@ func (c *WSClient) readRoutine() { // reset deadline for every message type (control or data) if c.readWait > 0 { if err := c.conn.SetReadDeadline(time.Now().Add(c.readWait)); err != nil { - panic(err) + c.Logger.Error("failed to set read deadline", "err", err) } } _, data, err := c.conn.ReadMessage() diff --git a/rpc/lib/server/handlers.go b/rpc/lib/server/handlers.go index 33cc00f2b..d9ab4790b 100644 --- a/rpc/lib/server/handlers.go +++ b/rpc/lib/server/handlers.go @@ -534,6 +534,7 @@ func (wsc *wsConnection) readRoutine() { } } }() + defer wsc.baseConn.Close() wsc.baseConn.SetPongHandler(func(m string) error { return wsc.baseConn.SetReadDeadline(time.Now().Add(wsc.readWait)) @@ -546,7 +547,7 @@ func (wsc *wsConnection) readRoutine() { default: // reset deadline for every type of message (control or data) if err := wsc.baseConn.SetReadDeadline(time.Now().Add(wsc.readWait)); err != nil { - panic(err) + wsc.Logger.Error("failed to set read deadline", "err", err) } var in []byte _, in, err := wsc.baseConn.ReadMessage() @@ -619,9 +620,7 @@ func (wsc *wsConnection) writeRoutine() { pingTicker := time.NewTicker(wsc.pingPeriod) defer func() { pingTicker.Stop() - if err := wsc.baseConn.Close(); err != nil { - panic(err) - } + wsc.baseConn.Close() }() // https://github.com/gorilla/websocket/issues/97 @@ -781,6 +780,6 @@ func writeListOfEndpoints(w http.ResponseWriter, r *http.Request, funcMap map[st w.WriteHeader(200) _, err := w.Write(buf.Bytes()) if err != nil { - panic(err) + // ignore error } } diff --git a/rpc/lib/server/http_server.go b/rpc/lib/server/http_server.go index a0f6d9acf..e918192b6 100644 --- a/rpc/lib/server/http_server.go +++ b/rpc/lib/server/http_server.go @@ -58,7 +58,7 @@ func WriteRPCResponseHTTPError(w http.ResponseWriter, httpCode int, res types.RP w.WriteHeader(httpCode) _, err = w.Write(jsonBytes) if err != nil { - panic(err) + // ignore error } } @@ -71,7 +71,7 @@ func WriteRPCResponseHTTP(w http.ResponseWriter, res types.RPCResponse) { w.WriteHeader(200) _, err = w.Write(jsonBytes) if err != nil { - panic(err) + // ignore error } } diff --git a/state/execution.go b/state/execution.go index a94bbdbf7..40169bcf7 100644 --- a/state/execution.go +++ b/state/execution.go @@ -288,10 +288,11 @@ func (s *State) indexTxs(abciResponses *ABCIResponses) { Tx: tx, Result: *d, }); err != nil { - panic(err) + s.logger.Error("Error with batch.Add", "err", err) } } if err := s.TxIndexer.AddBatch(batch); err != nil { + s.logger.Error("Error adding batch", "err", err) panic(err) } } diff --git a/types/part_set.go b/types/part_set.go index b68d05309..46387e353 100644 --- a/types/part_set.go +++ b/types/part_set.go @@ -36,7 +36,7 @@ func (part *Part) Hash() []byte { hasher := ripemd160.New() _, err := hasher.Write(part.Bytes) if err != nil { - panic(err) + // ignore error } part.hash = hasher.Sum(nil) return part.hash diff --git a/types/services.go b/types/services.go index e34d846b5..f025de79b 100644 --- a/types/services.go +++ b/types/services.go @@ -25,7 +25,7 @@ type Mempool interface { Size() int CheckTx(Tx, func(*abci.Response)) error Reap(int) Txs - Update(height int, txs Txs) + Update(height int, txs Txs) error Flush() TxsAvailable() <-chan int @@ -42,7 +42,7 @@ func (m MockMempool) Unlock() {} func (m MockMempool) Size() int { return 0 } func (m MockMempool) CheckTx(tx Tx, cb func(*abci.Response)) error { return nil } func (m MockMempool) Reap(n int) Txs { return Txs{} } -func (m MockMempool) Update(height int, txs Txs) {} +func (m MockMempool) Update(height int, txs Txs) error { return nil } func (m MockMempool) Flush() {} func (m MockMempool) TxsAvailable() <-chan int { return make(chan int) } func (m MockMempool) EnableTxsAvailable() {}