# Preventing socket events from injecting themselves into the stack

**URL:** <https://forum.xojo.com/t/preventing-socket-events-from-injecting-themselves-into-the-stack/85300>\
**Category:** General\
**Created:** [May 10, 2025, 6:34am UTC](https://forum.xojo.com/t/preventing-socket-events-from-injecting-themselves-into-the-stack/85300 "2025-05-10T06:34:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [May 10, 2025, 6:34am UTC](https://forum.xojo.com/t/preventing-socket-events-from-injecting-themselves-into-the-stack/85300/1 "2025-05-10T06:34:28Z")

</div>

So many years using Xojo, and still finding new weird behaviors. I have console app that manages a bunch of sockets. Socket events fire on the main thread, unless you’re polling them. I’m not. Being a console app, I have a single DoEvents loop in the Run event handler. While I fully understand that I cannot count on event order, I’m having a situation where the error event of a socket sometimes gets added to the existing stack instead of waiting for the next iteration of the event loop.

Take this log output for example:

```auto
ExchangeSocket.mRconSocket_Disconnected: RCON has disconnected
ExchangeSocket.Error: Unexpected disconnect, calling Cleanup
ExchangeSocket.Cleanup: Cleanup started
ExchangeSocket.Cleanup: Cleanup finished
ExchangeSocket.mRconSocket_Disconnected: RCON cleanup complete

```

These are independent methods that I’ve confirmed are definitely both firing on the main thread. But **while** executing the code for `ExchangeSocket.mRconSocket_Disconnected`, `ExchangeSocket.Error` has fired. You can see that in this stack trace here:

 ![SCR-20250510-cueg](https://forum.xojo.com/uploads/default/original/3X/7/e/7e59dbd39683f3ca7d6ef5e4b78f2c477658eaa1.png)

The code is in `Sentinel.Service.SaveTo`, but `ExchangeSocket.Event.Error` has just decided it’s going to be run. I’d expect at a loop boundary, but in this case the calling line is a SelectSQL. And no, the line doesn’t **actually** call the event. That’s just where the event has been injected into the stack.

 ![SCR-20250510-cicb](https://forum.xojo.com/uploads/default/original/3X/8/d/8d6470a828c7ea3951d0a03a1b410bc5760d643a.jpeg)

This is code from `ExchangeSocket.mRconSocket_Disconnected`. You can see the DebugPrint at the top, though the Terminal is obscuring a successful usage of `Self.mService` just above that if. When that line was called, `Self.mService` was not nil. But then “Unexpected disconnect” is called, which I know sets `Self.mService` to nil as part of cleanup, and then back in `mRconSocket_Disconnected`, `Self.mService` is now nil.

This is macOS ARM 2025r1.1. I can’t even think of a way to counter this. It just shouldn’t be happening.

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [May 10, 2025, 7:09am UTC](https://forum.xojo.com/t/preventing-socket-events-from-injecting-themselves-into-the-stack/85300/2 "2025-05-10T07:09:36Z")

</div>

Ok, I know what’s happening. The key clue is this line:

> [@Thom\_McGrath](#):
>
> I’d expect at a loop boundary, but in this case the calling line is a SelectSQL

It’s the Multithreaded property of the database. When true (the default) your SQL statements can act as event loops.

Anybody have any idea how Multithreaded = False behaves inside a preemptive thread?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [May 10, 2025, 7:57am UTC](https://forum.xojo.com/t/preventing-socket-events-from-injecting-themselves-into-the-stack/85300/3 "2025-05-10T07:57:33Z")

</div>

Well, if the SQL code doesn’t yield, that should not cause trouble since preemptive threads don’t need to call yield. When other threads do yield, they may do the socket events.
