diff --git a/core/src/main/java/com/turn/ttorrent/client/peer/PeerExchange.java b/core/src/main/java/com/turn/ttorrent/client/peer/PeerExchange.java index 12b94d73a..fab7a5fa5 100644 --- a/core/src/main/java/com/turn/ttorrent/client/peer/PeerExchange.java +++ b/core/src/main/java/com/turn/ttorrent/client/peer/PeerExchange.java @@ -350,7 +350,14 @@ public void run() { } buffer.rewind(); - + + if (stop) { + // The buffer may contain the type from the last message + // if we were stopped before reading the payload and cause + // BufferUnderflowException in parsing. + break; + } + try { PeerMessage message = PeerMessage.parse(buffer, torrent); logger.trace("Received {} from {}", message, peer);