Codea has CloudKit issues

Claude has gotten some of my Codea Xcode projects to work with CloudKit, through a highly kludgey process imho.

Claude’s description of the problem and what it did to fix it:

Obstacle and workaround. On iPadOS 26.6.1 and iOS 18.7.11 devices, any Lua completion handler passed to a framework call (we hit it with CloudKit saveRecord/fetchRecord, but GameKit has the same exposure) can crash the app with SIGSEGV inside LuaKit, usually in luaG_typeerror or luaE_checkcstack, entered from __luafunc_getproperty_block_invoke. When the handler is delivered on the main queue while the render thread is inside an objc.getproperty hop, it runs re-entrantly on the same lua_State with a corrupted stack. The console shows “attempt to call a boolean/nil value” from objc.getproperty, and the process then dies, or the render thread wedges. It only appears during normal play. Calls driven one at a time from a quiet frame always worked, which hid it for a while. Making the handler body just queue the work, issuing calls last in the frame, or queuing requests one at a time did not help. The workaround. We now keep Lua out of the callback path completely: the framework calls are in a Swift @objc class that never calls Lua, results go into a locked queue, and Lua reads them by polling a zero-argument method that returns JSON. That has run stably through full two-device games and soak tests.

I asked Claude what the simplest Codea-side fix would be, and it suggested this:

Simplest fix on Codea’s end. Don’t run a queued Lua callback from inside a nested bridge call (a property read or method call that hops to the main thread). Park the callback, and run it only at a safe point, such as the start of the next draw()/touched(), or when the Lua call stack is empty. The getproperty hop’s dispatch_async_and_wait drains the main queue, which is how a pending callback ends up executing mid-call. A single flag, “a bridge call is in progress, so queue instead of run”, would be enough. Two related issues that cost us time and that you may want to fix or document: a callback parameter whose name contains a digit (e.g. o__err2) is silently never invoked, and setting a block-valued property from Lua (e.g. CKModifyRecordsOperation.modifyRecordsCompletionBlock) corrupted the bridge.

@Steppers , @jfperusse and/or @sim: would that be an easy fix?