From fce1fc7990ee06b0b3968b7682166d0187cc78c7 Mon Sep 17 00:00:00 2001 From: bwzhou Date: Thu, 30 Jun 2016 18:03:45 -0700 Subject: [PATCH] Fixed a BufferUnderflowException in message parsing when PeerExchange is closing --- .../java/com/turn/ttorrent/client/peer/PeerExchange.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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);