macOS crash when closing MySQL DB connection

Hi there,

I have a macOS desktop app crashing sometimes when closing the MySQL DB connection.

The app was changed recently to better handle DB connections, using a different DB connection for every thread running. The DB connection closes when the thread ends (but for the main thread).

It works fairly well but in two windows, always the same, where upon close the app crashes directly without UnhandledException. Most windows are using a special listbox using a thread to lazy load the data into the listbox. Only 2 are crashing when closing. These 2 are closing and then immediately opening another window.

My customer uses only iMacs, most of them ARM, macOS ranging from BigSur to Sequoia. I can’t reproduce the crash on my own dev and test machines (iMacPro Intel and macMini M1).

App is a desktop app, build using Xojo 2023r2. App contains 5 helper apps (console apps).

App is notarized using AppWrapper, builds are generated for Intel64 and ARM64.

Database is MySQL, using pure Xojo here, not MBS SQL.

Can someone help me decrypting the crash log below ?

Thanks for your help !

full log here dfDiveShop-2026-01-23-002852.ips.zip (9.7 KB)

Top of the crash log :

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               dfDiveShop [40835]
Path:                  /Applications/dfDiveShop.app/Contents/MacOS/dfDiveShop
Identifier:            com.gate61.dfdiveshop
Version:               4.0.7 (4.0.7)
Code Type:             ARM-64 (Native)
Parent Process:        launchd [1]
User ID:               501

Date/Time:             2026-01-23 00:28:48.3012 +0100
OS Version:            macOS 15.5 (24F74)
Report Version:        12
Anonymous UUID:        72938CAE-68FA-E53D-09FE-94A7538B9EEE


Time Awake Since Boot: 130000 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000

Termination Reason:    Namespace SIGNAL, Code 6 Abort trap: 6
Terminating Process:   dfDiveShop [40835]

Application Specific Information:
abort() called


Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib        	       0x19f959388 __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x19f99288c pthread_kill + 296
2   libsystem_c.dylib             	       0x19f89bc60 abort + 124
3   libsystem_malloc.dylib        	       0x19f7a0174 malloc_vreport + 892
4   libsystem_malloc.dylib        	       0x19f7cba00 malloc_zone_error + 100
5   libsystem_malloc.dylib        	       0x19f7ad550 <deduplicated_symbol> + 40
6   MySQLCommunityPlugin.dylib    	       0x111d278d0 0x111cf4000 + 211152
7   MySQLCommunityPlugin.dylib    	       0x111cfe548 0x111cf4000 + 42312
8   MySQLCommunityPlugin.dylib    	       0x111d03e8c 0x111cf4000 + 65164
9   MySQLCommunityPlugin.dylib    	       0x111e47e68 0x111cf4000 + 1392232
10  XojoFramework                 	       0x104b96064 databaseClose + 48
11  dfDiveShop                    	       0x100d561e0 Database.Close%%o<Database> + 16
12  dfDiveShop                    	       0x102fab5a0 myDBconnection.!freePool%%s + 3392
13  dfDiveShop                    	       0x101ab6810 ccDocuments.ccDocuments.Event_Close%%o<ccDocuments.ccDocuments> + 476
14  dfDiveShop                    	       0x100f3e93c EmbeddedWindowControl.Event_Close%%o<EmbeddedWindowControl> + 16
15  XojoFramework                 	       0x104c07230 ControlClose + 396
16  XojoFramework                 	       0x104b881fc RuntimeView::UnifiedClose(bool) + 568
17  dfDiveShop                    	       0x100ef999c Window.Close%%o<Window> + 44
18  dfDiveShop                    	       0x1018ee63c ccMenuPOS.ccMenuPOS.placeCC%%o<ccMenuPOS.ccMenuPOS>v + 496
19  dfDiveShop                    	       0x1018ec4b0 ccMenuPOS.ccMenuPOS.displayPage%%o<ccMenuPOS.ccMenuPOS>svb + 4560
20  dfDiveShop                    	       0x101fa6ae4 wMenu.wMenu.forceToolbarActionParam%%o<wMenu.wMenu> + 960
21  dfDiveShop                    	       0x100fb7894 _CallLaterTimer.Event_Action%%o<_CallLaterTimer> + 212
22  XojoFramework                 	       0x104c87140 0x104a10000 + 2584896
23  CoreFoundation                	       0x19fa96e14 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 32
24  CoreFoundation                	       0x19fa96ad4 __CFRunLoopDoTimer + 980
25  CoreFoundation                	       0x19fa96610 __CFRunLoopDoTimers + 332
26  CoreFoundation                	       0x19fa7ca18 __CFRunLoopRun + 1848
27  CoreFoundation                	       0x19fa7bc58 CFRunLoopRunSpecific + 572
28  HIToolbox                     	       0x1ab51027c RunCurrentEventLoopInMode + 324
29  HIToolbox                     	       0x1ab5134e8 ReceiveNextEventCommon + 676
30  HIToolbox                     	       0x1ab69e484 _BlockUntilNextEventMatchingListInModeWithFilter + 76
31  AppKit                        	       0x1a39a3ab4 _DPSNextEvent + 684
32  AppKit                        	       0x1a43425b0 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 688
33  XojoFramework                 	       0x104af486c 0x104a10000 + 936044
34  XojoFramework                 	       0x104af48f4 0x104a10000 + 936180
35  dfDiveShop                    	       0x100f325a8 Application._CallFunctionWithExceptionHandling%%o<Application>p + 164
36  XojoFramework                 	       0x104c7ef80 CallFunctionWithExceptionHandling(void (*)()) + 180
37  XojoFramework                 	       0x104af4890 0x104a10000 + 936080
38  AppKit                        	       0x1a3996c64 -[NSApplication run] + 480
39  XojoFramework                 	       0x104c7d7dc RuntimeRun + 48
40  dfDiveShop                    	       0x10109b6c4 REALbasic._RuntimeRun + 28
41  dfDiveShop                    	       0x102fb193c _Main + 528
42  dfDiveShop                    	       0x102faea84 main + 36
43  dyld                          	       0x19f5f2b98 start + 6076

A window is closing. The embedded container of the window ccDocuments is closing the database which is freaking the app out. What is your code in ccDocuments.Close?

Sounds like you call close twice on the database. Could that be?

Or something else references it.

in general I would not close it at all. The destructor will do that on the end.

Thanks Beatrix.

The code is the same for all containers, on Close event they are calling a shared method ‘freePool’ of the db pool handler class.

The db pool handler class contains a shared dictionary property (key is thread id) and the dbConnection is closed like this:

      pool.dbConnection.Close
      pool.dbConnection = Nil '20260118 added to prevent crash ?

pool.dbConnection is a sub-class of MySQLCommunityServer.

The full method freePool is here:

If connectPool.HasKey(forThreadId) Then
  
  Dim pool As myDBconnection = connectPool.Value(forThreadId)
  
  
  If pool <> Nil And pool.dbConnection <> Nil And pool.dbWeakRef <> Nil Then ' And pool.dbConnection.isConnected Then
    Try
      pool.dbConnection.Close
      pool.dbConnection = Nil '20260118 added to prevent crash ?
      
    Catch err As DatabaseException
      logError(CurrentMethodName + ": ERROR freeing and closing DB for ThreadId " + forThreadId + ": " + err.Message + " (" + err.ErrorNumber.ToString + ")", False)
    End Try
  Else
    logError(CurrentMethodName + ": connectPool entry for ThreadId " + forThreadId + " already Nil or closed on ip " + pool.dbConnection.Host, True)
  End If
  
  Try
    connectPool.Remove(forThreadId)
    
  Catch err As RuntimeException 
    logError(CurrentMethodName + ": ERROR removing connection from pool for ThreadId " + forThreadId + ": " + err.Message + " (" + err.ErrorNumber.ToString + ")", False)
  End Try
  
Else
  If DebugBuild Then logError(CurrentMethodName + ": connectPool entry for ThreadId " + forThreadId + " already closed, NOT in  myDBconnection", True)
End If

Else
' thread
logError(CurrentMethodName + ": Connection Pool ThreadId " + forThreadId + " ignored ...", True)

End If

What is strange is that I can’t reproduce it on both machines Intel and M1 (Sequoia and Tahoe). If calling it twice was the issue I should have the same error on my machines .

db.Close happens only in the freePool method, never in the containers themselves.

You said they’re in threads. Cooperative or Preemptive?

I guess that the thread still has ownership of the connection and doing things while you from another thread tried to close it (sometimes vice-versa). You must synchronize the task. Like setting a flag “close” to a connection pool manager (from the thread, also managing simultaneous access to this flag) and wait the pool manager report back it is released inspecting a status “closed” and then continue and release the thread resources. Something by those lines. Just one close at one point, exchange messages.

Hi Greg, they are all cooperatives.

I don’t think Xojo2023r2 has premptive threads anyway.

You may be right.

I’ll try adding a critical section to prevent 2 db.close to be called at the same time.

The second would have to wait until the critical section is release and could then find out it’s already close…

Even cooperative, if Xojo yelds time to another thread at some improper timeframe one thread can capture a processing while another one still have dependencies going on. Synchronize it in a better way assuring a controlled release of resources.

It can be worse than that. You must not call close in 2 different places “in parallel” or “perfect sequence”. If you have a pool manager, only it should create and destroy connections. And you should ask your manager a create and a close and wait for a status change before releasing your copy of the reference in another thread (that Xojo luckily will avoid firing another release on the already released one, or you have a framework bug).