Problem refreshing listenKey

I create a websocket to listen to updates on my orders. To do so, I create a listenKey, as required. Once every 30 minutes (1800 seconds), I send a refresh message to keep the listenKey alive, and that seems to go fine for many iterations. But after approximately 21.5 hours of trading against testnet, something goes wrong. My refresh attempt fails. Shortly thereafter, the listenKey apparently expires and the websocket throws an error. At that point, my code attempts to cancel the old listenKey and then generate a new listenKey, but BOTH of those attempts fail also and, so, I’m unable to continue listening. At that point, I have little choice but to terminate the process. If I immediately restart, however, the code IS able to generate a listenKey and it is able to resume listening.

The following occurred to me: By the time I try to generate a new listenKey, the old websocket object has passed out of context and been destroyed. But maybe I need to let a few seconds pass before asking for a new listenKey to enable Binance’s API to close out the old websocket. This doesn’t seem to me that it should be necessary, though. I think it’s at least SUPPOSED to be possible to generate a new listenKey even if an existing websocket is running. So this is a long-shot theory.

Any ideas as to what’s wrong here and how I can fix it? Is it possible this is a feature of testnet – but not production – that is designed to prevent users from running connections infinitely?

I want my trading process to run for many days on end – maybe infinitely. I need to be able to keep refreshing my listenKey and/or generate a new one on the fly without having to pull down the process and restart it.

Thanks in advance for any help.