Finalizers are dangerous


Yury Batrakov
 

Classification: Public
Hi team,

Sorry for telling bad news in holidays but I have found a major issue with Java API - JVM may crash if GC comes after Mama.close() method. Here's code sample to reproduce it:

import com.wombat.mama.Mama;
import com.wombat.mama.MamaMsg;

public class Test {
private static MamaMsg getMessage() {
return new MamaMsg();
}

public static void main(String[] args) {
Mama.loadBridge("...");
Mama.open();

getMessage(); // Creating MamaMsg object without reference

Mama.close(); // Payload bridge is destroyed here
System.gc();
System.runFinalization(); // Calling MamaMsg.destroy() which delegates the destruction to deleted payload bridge
}
}

Stack trace:
#12 0x00007fc494a095f0 in ?? ()
#13 0x00007fc496ac1cf4 in mamaMsg_destroy (msg=0x7fc4900c90a0) at mama/c_cpp/src/c/msg.c:127
#14 0x00007fc496d70b7f in Java_com_wombat_mama_MamaMsg__1destroy (env=0x7fc4b00039f8, this=0x7fc49779d710) at mama/jni/src/c/mamamsgjni.c:3882
#15 0x00007fc4bae7e494 in ?? ()

Problematic frame:
#13 0x00007fc496ac1cf4 in mamaMsg_destroy (msg=0x7fc4900c90a0) at mama/c_cpp/src/c/msg.c:127
127 if (MAMA_STATUS_OK != impl->mPayloadBridge->msgPayloadDestroy (impl->mPayload))

impl->mPayloadBridge is destroyed here.

Similar crash occurs when finalizing subscriptions - they also need entitlements bridge to be available but Mama.close() deletes it too.

The workaround is to call destroy() method for each message/subscription created but this actually nullifies the need for finalize() methods. I would delete all them from MAMA code.


---
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

Please refer to https://www.db.com/disclosures for additional EU corporate and regulatory disclosures and to http://www.db.com/unitedkingdom/content/privacy.htm for information about privacy.

Join Openmama-dev@lists.openmama.org to automatically receive all group messages.