rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.77k stars 1.22k forks source link

Closing the bot session with BotSession.stop() throws a Java Exception #329

Closed el-duderino5 closed 6 years ago

el-duderino5 commented 7 years ago

Hey guys. Just documenting this error here as I have previously mentioned this in the telegrambots channel and there was no answer on whether there are plans to fix it:

When I call BotSession.stop() on a LongPollingBot that is active, I receive this error:

**SEVERE: BOTSESSION
java.net.SocketException: Socket closed**
at java.net.SocketInputStream.read(SocketInputStream.java:204)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at org.apache.http.impl.conn.LoggingInputStream.read(LoggingInputStream.java:84)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:282)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:165)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.getUpdatesFromServer(DefaultBotSession.java:243)
at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.run(DefaultBotSession.java:186)

Is someone working on planning or solving this error? My guts tell me that there is a problem with the socket at the moment of stopping the session - Since it's longpolling, it is constantly (and feel free to correct me if I'm wrong) using getUpdates() to get outstanding updates, at the moment of closing the BotSession the socket is still trying to read from the Telegram server and, since the session is stopped, the operation abruptally ends without closing the socket properly first or gracefully interrupting the read operation.

This error may be related to this past issue #189

rubenlagus commented 7 years ago

Isn't it a duplicated of #168 ?

el-duderino5 commented 7 years ago

@rubenlagus Yes, it appears to be so. Still, I think something must be done about it. I don't know what the implications of leaving this bot as is are, but I think the correct way is to somewhat shutdown that socket gracefully, either if is in an idle state or reading. This issue is also a duplicate of #189 , but I just wanted to know if something is being done about it and simply offer some asummptions or suggestions about what should be done about it. I was always taught that you close resources as soon as you don't need them/done with them, as it may cause memory leaks and who knows what else.

rubenlagus commented 6 years ago

@el-duderino5 Is this exception still thrown with latest version?