Strange error thrown while writing with AVAudioFile

I'm encountering a strange and intermittent error thrown by AVAudioFile.write(from:) — the error domain is com.apple.coreaudio.avfaudio and the code is -40. According to this thread, this error means The Audio Buffer has Become Miscoded, though I'm not sure what this actually signifies.

This error manifests while my app is rendering to audio from an offline AVAudioEngine to a file, which happens on a serial dispatch queue. The error is not deterministic — most of the time it doesn't happen at all, and when it does, my app automatically retries and most often the rendering succeeds after one or more retries. Occasionally the error keeps coming up until the app is terminated and relaunched.

The file I am writing to is created using the fileFormat.settings of an audio file I am reading from, and the PCM buffer is created using the processingFormat of that file, so they should always be compatible. In practice the format is always the same (stereo deinterleaved PCM 192kHz 32-bit float in a CAF container).

I have noticed that since iOS 16.0 there seems to be a bug affecting writing with AVAudioFile which is that the framePosition property of the file being written to does not update immediately upon calling AVAudioFile.write(from:) to reflect the most recent write. I have stopped relying on that property when writing to files, but I wonder if these issues are somehow related ... (possibly, but not necessarily...)

Any help or advice much appreciated.

~ Milo