MWEngine icon indicating copy to clipboard operation
MWEngine copied to clipboard

Thread issue maybe?

Open vustav opened this issue 2 years ago • 10 comments

I'm using handleNotification() in your StateObserver class (just copied from your example) to update things in realtime and create automations (this includes modifying things in the engine, ex. changing a parameter on an fx step 3-6). When just calling the relevant objects I get occasional null pointer dereference crashes. Calling them from the UI-thread is way more stable. I'm not good at threads, I guess I just vaguely get the concept, but it seems engine updates should execute in whatever thread the engine used to call them to avoid conflicts like this? Or does it get weird C -> Java? Or am I completely wrong here and just need to write better applications =)? (that last happy face does not mean I don't see it as an option, as I said I don't know threads).

vustav avatar Jun 14 '22 20:06 vustav

The crash I'm getting is this one and I'm somewhat able to reproduce it when the sequencer goes from last step -> 0:

2022-06-16 11:33:26.928 32383-32383/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: Build fingerprint: 'google/sdk_gphone_x86/generic_x86:8.0.0/OSR1.170901.043/4456315:user/release-keys' 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: Revision: '0' 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: ABI: 'x86' 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: pid: 32329, tid: 32356, name: Thread-2 >>> com.kiefer.llppdrums <<< 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: Cause: null pointer dereference 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: eax 91b7ea00 ebx 928f48a8 ecx ad8595f0 edx 924ff330 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: esi 000178bf edi 00017610 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: xcs 00000073 xds 0000007b xes 0000007b xfs 0000003b xss 0000007b 2022-06-16 11:33:26.929 32383-32383/? A/DEBUG: eip 00000000 ebp 924ff358 esp 924ff2cc flags 00010286 2022-06-16 11:33:26.933 32383-32383/? A/DEBUG: backtrace: 2022-06-16 11:33:26.933 32383-32383/? A/DEBUG: #00 pc 00000000 2022-06-16 11:33:26.933 32383-32383/? A/DEBUG: #01 pc 000c59cb /data/app/com.kiefer.llppdrums-fB9yJKWR7NdQvDCh5i7CIg==/base.apk (offset 0x2c06000) 2022-06-16 11:33:26.933 32383-32383/? A/DEBUG: #02 pc 000b63b2 /data/app/com.kiefer.llppdrums-fB9yJKWR7NdQvDCh5i7CIg==/base.apk (offset 0x2c06000) 2022-06-16 11:33:26.933 32383-32383/? A/DEBUG: #03 pc 000cb2b2 /data/app/com.kiefer.llppdrums-fB9yJKWR7NdQvDCh5i7CIg==/base.apk (offset 0x2c06000) 2022-06-16 11:33:26.933 32383-32383/? A/DEBUG: #04 pc 000cb00c /data/app/com.kiefer.llppdrums-fB9yJKWR7NdQvDCh5i7CIg==/base.apk (offset 0x2c06000) 2022-06-16 11:33:26.933 32383-32383/? A/DEBUG: #05 pc 00013b89 /system/lib/libaaudio.so (_ZN6aaudio17AudioStreamLegacy21processCallbackCommonEiPv+201) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #06 pc 000153eb /system/lib/libaaudio.so (_ZN6aaudio16AudioStreamTrack15processCallbackEiPv+59) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #07 pc 000139f7 /system/lib/libaaudio.so (ZL26AudioStreamLegacy_callbackiPvS+39) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #08 pc 00046ea1 /system/lib/libaudioclient.so (_ZN7android10AudioTrack18processAudioBufferEv+2465) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #09 pc 000491df /system/lib/libaudioclient.so (_ZN7android10AudioTrack16AudioTrackThread10threadLoopEv+207) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #10 pc 0001044f /system/lib/libutils.so (_ZN7android6Thread11_threadLoopEPv+207) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #11 pc 0000fca6 /system/lib/libutils.so (ZN13thread_data_t10trampolineEPKS+246) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #12 pc 000709b5 /system/lib/libc.so (_ZL15__pthread_startPv+53) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #13 pc 0002050b /system/lib/libc.so (__start_thread+75) 2022-06-16 11:33:26.934 32383-32383/? A/DEBUG: #14 pc 0001eda6 /system/lib/libc.so (__bionic_clone+70) 2022-06-16 11:33:27.651 1558-1558/? E//system/bin/tombstoned: Tombstone written to: /data/tombstones//tombstone_00


In this case nothing except for some UI-elements is modified through handleNotification(), but it's much less frequent when calling from the UI-thread so it still feels like it has something to do with that.

No idea if this is the engine or if this info is relevant to you. I've had this for a long time and made an issue before where I blamed muting, which was wrong. I can upload the tombstone if you want it.

vustav avatar Jun 16 '22 10:06 vustav

I finally got firebase working and this is what I'm getting: crash

vustav avatar Jun 19 '22 19:06 vustav

In the crash I was able to reprodice I've deleted steps (which means deleted events) while the engine was paused (and randomized other things) and the crash happens when the sequencer goes from last step -> 0.

Just to be clear: I have no idea if this is thread related, I just noticed less crashes in the UI-thread.

vustav avatar Jun 19 '22 19:06 vustav

When deleteing tracks while the engine is running I'm getting the exact same crash exept for step 0 in firebase that says "missing". All other steps are identical.

vustav avatar Jun 21 '22 11:06 vustav

A little note here: deleteing tracks while the engine is running does not mean deleting events. I've had trouble deleting events with the engine running before, so right now I store them and delete all when the engine is paused. Same goes for fxs and instruments. This is an old fix and I don't know if it's needed anymore, I will investigate this soon.

vustav avatar Jul 02 '22 15:07 vustav

So I had some time to look into this.

Debouncing on the UI thread may work as as the handler will not be executed synchronously with the sequencer, which reduces risk of concurrent operations on removed items.

So just to be clear : the action you re performing is removing events from the sequencer while it is running right (using removeFromSequencer) ? (Note: this should totally work, so this is a definitive bug if this is the case). If you are doing other things maybe you can post a snippet of the changes you are executing ? Judging from the stack trace I have assumed it's related to event removal though.

If you push the latest version of the repository and rebuild the library, is the issue addressed ?

igorski avatar Jul 24 '22 11:07 igorski

Can't assemble the latest version with #define RECORD_DEVICE_INPUT and #define RECORD_TO_DISK commented away. utilities/diskwriter.h is never included and still called further down. Will try with them enabled but thaught you should know.

Tried for a while with the version I had before the latest and had zero crashes. I could pretty reliably recreate it before, atleast once every 10th try or so, so might be that it was solved allready.

Sorry for the vagueness, been away for three weeks.

vustav avatar Jul 26 '22 13:07 vustav

utilities/diskwriter.h is never included and still called further down in audioengine.cpp!!

vustav avatar Jul 26 '22 13:07 vustav

Ah sorry that's embarrassing 🙈

A recent improvement to the recording API left this lingering and I didn't check the impact in the case RECORD_DEVICE_INPUT|RECORD_TO_DISK was disabled in global.h. I have pushed a change that removed the old import guard (DiskWriter will then be included - as it was before, but in a different file as it depends on the same imports as file import/WaveReader - but the recording code will remain dormant during render, as before).

igorski avatar Jul 27 '22 05:07 igorski

In the crash I could reproduce I had removed/added events to the engine before starting the sequencer. I think the events are irrelevant. The updates are simple, like increasing the delay time on certain steps, using handleNotification(). The crash always occured at step 0 (or maybe the last step) but nothing special in regards to the engine is happening there. Blaming it on threads was just a thaught I had.

vustav avatar Jul 28 '22 21:07 vustav