Inconsistent crash-on-launch while calling MLModel.compileModel(at: url)

Okay here's a strange one.

I have a project in which I compile a couple .mlmodel files on first launch of the app. For the vast majority of our users this is working just fine. However, for a few users the app is crashing on every launch at the point where I call MLModel.compileModel(at: newFileURL).

The error that's getting thrown is: Error during compilation of model: Error Domain=NSCocoaErrorDomain Code=512 "The file couldn’t be saved."

Once a user's device begins crashing here it will continue crashing here until a complete delete-reinstall of the app. A simple build-and-run will not cause the issue to go away (which makes it debuggable to an extent). The crashing issue even persists between app updates installed through the App Store.

I've tried doing this compilation on different threads, changing the name of the model, etc. Nothing seems to resolve it except a complete delete and reinstall of the app.

So the open questions are:
  • Why would this method fail to write the file?

  • Why are only some users getting into this crashing state?

  • Why does a full delete-reinstall fix it?

Any help, even just wild guesses, would be appreciated because I'm pretty stumped.

Thanks!
When this happens, is it possible to save another large file (doesn't matter what is inside)? If not, perhaps the device's storage is full.
Device storage isn't full but that's a good idea about switching out some of the .mlmodel files. I'll try that next time I get my hands on a device that's gotten into this state.

Thanks
No answers just more debugging details -

I tried using different MLModels (from 1MB-30MB in size). Once it gets into this crashing state it doesn't seem to care if you change the model files.

I've seen all types of builds/deployments get into this crashing state. Xcode debug builds, TestFlight builds, and App Store builds have all ended up in this state. Still the only known fix is a full delete and reinstall.
Just another debugging detail -
I tried moving the MLModels into the Application Support directory, rather than the Documents directory, (FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
vs FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first) and it did not prevent the crash.
I think I've found the issue. It would seem there is a limit to the number of temporary files allowed in an app container and the MLModel.compileModel(at: newFileURL) call creates temporary files but does not cleanup. Whatever process is responsible for managing the temporary folder cannot be relied upon. See nshipster.com/temporary-files for a good overview of best practices.
@wvdk I would love to understand your use case better. What's the idea behind compiling the model multiple times on the same device? Are you compiling different models?

It is recommended that app save the compiled model in a permanent storage before using it for inference. It is also a good idea to minimize number of model compilations on the device.

https://developer.apple.com/documentation/coreml/core_ml_api/downloading_and_compiling_a_model_on_the_user_s_device
Inconsistent crash-on-launch while calling MLModel.compileModel(at: url)
 
 
Q