certstream-python: Error connecting to CertStream - Connection is already closed
I implemented this tool, following the given examples. I found out that sometimes ( usally every 2 minutes, more or less) the connection drops and it takes almost a minute to reconnect to the stream.
I don’t know why this happens and this could probably lead to missing some hits i’m looking for.
Here’s an example of log ( integrated with python logging module )
[ERROR:certstream] 2020-09-02 16:20:31,002 - Error connecting to CertStream - Connection is already closed. - Sleeping for a few seconds and trying again...
[INFO:certstream] 2020-09-02 16:20:36,097 - Connection established to CertStream! Listening for events...
[...]
[ERROR:certstream] 2020-09-02 16:23:10,796 - Error connecting to CertStream - Connection is already closed. - Sleeping for a few seconds and trying again...
[INFO:certstream] 2020-09-02 16:23:15,908 - Connection established to CertStream! Listening for events...
[...]
[ERROR:certstream] 2020-09-02 16:25:11,015 - Error connecting to CertStream - Connection is already closed. - Sleeping for a few seconds and trying again...
[INFO:certstream] 2020-09-02 16:25:16,182 - Connection established to CertStream! Listening for events...
[...]
[ERROR:certstream] 2020-09-02 16:27:17,051 - Error connecting to CertStream - Connection is already closed. - Sleeping for a few seconds and trying again...
[INFO:certstream] 2020-09-02 16:27:22,194 - Connection established to CertStream! Listening for events...
[...]
[ERROR:certstream] 2020-09-02 16:29:01,343 - Error connecting to CertStream - Connection is already closed. - Sleeping for a few seconds and trying again...
[INFO:certstream] 2020-09-02 16:29:06,522 - Connection established to CertStream! Listening for events...
Any suggestion about how to fix this?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 9
- Comments: 29 (3 by maintainers)
Argh, well this is disappointing. 😦 I am hitting this problem too and can only reach the same conclusion as @kres20 that the
certstream.calidog.ioservice is just basically unusable.I noticed
certstream.calidog.iois actually Cloudflare. In Wireshark packet capture on my client, I can see the Cloudflare frontend server is just suddenly closing the TCP connection for some reason. No shutting down of the Websocket session or the TLS session, but a (mostly) normal 4-way close handshake of the outer TCP connection. (Abnormally, the Cloudflare server waits for exactly 10 seconds before sending its very lastACK, despite my client dutifully attempting several retransmissions of its lastFINthat the server is waiting for.)When I add
sslopt={'suppress_ragged_eofs': False}to thecertstream.listen_for_eventsparameters, the issue causes an exception that matches what I saw in packet capture:ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2633).When I use my preferred public DNS resolver (149.112.121.10),
certstream.calidog.ioresolves to172.64.80.1for me. This is a Cloudflare IP. Traceroute from my client and from a couple other places suggests that it’s an anycast IP that exists at/near multiple different Internet exchanges.When I use other public DNS resolvers,
certstream.calidog.ioresolves to different Cloudflare IPs for me -172.67.151.243and104.21.40.132. When I force my client to connect to these IPs, the problem still happens, but anecdotally seems to occur far less frequently than with172.64.80.1.I’m skeptical that IP anycast is a suitable CDN strategy for services requiring permanent TCP connections, such as the Calidog Certstream service. IMO this problem is likely a completely normal side-effect of the chosen CDN strategy that the Calidog service is using. 😦
@katesaikishore @Fitblip I set up the certstream server too (https://github.com/CaliDog/certstream-server), and connect there like
certstream.listen_for_events(print_callback, url='ws://localhost:4000/'), and it works fine without any errors. Therefore this above error is caused by certstream.calidog.io.Thank you @Fitblip for your reply! I followed your steps:
As you can see I still get the error
Error connecting to CertStream - Connection to remote host was lost, which is another error to the previously mentionedConnection is already closed.Also in my custom software where I use certstream as a library:
Is this the second part you addressed in your comment? So this could only be fixed with a regular
ws.send('.')? If so, I’ll try to implement this as soon as I find some time.@Fitblip @d-Rickyy-b I implemented ws.send in cli.py as following.
But, still get the same error.
I would like to prevent data loss caused by this error, do you have any ideas?
Just as a point of reference, today I updated to certstream 1.12 (python 3.9.6 on Linux 64bit) and it works fine.
This is fixed upstream https://github.com/websocket-client/websocket-client/pull/594
I for my part am using your
certstream.calidog.ioserver - at least for development.The error message is always “Connection is already closed.”. There are basically two places where that exception is raised:
websocket/_app.py
websocket/_socket.py
For me the disconnects only appear within the latter (recv) call.
It reads 0 bytes
This is the full stack trace: