Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ffi] Data corruption from native thread #59635

Closed
LeoBound opened this issue Nov 29, 2024 · 5 comments
Closed

[ffi] Data corruption from native thread #59635

LeoBound opened this issue Nov 29, 2024 · 5 comments
Labels
area-native-interop Used for native interop related issues, including FFI. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@LeoBound
Copy link

Hi,
I'm having an issue where a NativeCallable.listener callback is receiving garbled data.
My callback is void Function(Pointer<Char> json) => json.cast<Utf8>().toDartString();
This fails ~80% of the time as it fails to decode the UTF-8 in the call to toDartString(). This string should always be valid UTF-8, and indeed in the remaining 20% of the time it succeeds and behaves as expected.

I believe it is similar to the issue described here.

Is there a workaround for this? I think the proposed NativeCallable.blocking might solve it but in the meantime is there anything I can do from the dart side?

Dart SDK version: 3.4.4

Thanks

@dart-github-bot
Copy link
Collaborator

Summary: NativeCallable listener callback receives corrupted data (~80% failure rate) when converting UTF-8 Pointer<Char> to Dart String. User suspects thread race condition, similar to a known Flutter issue.

@dart-github-bot dart-github-bot added area-native-interop Used for native interop related issues, including FFI. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Nov 29, 2024
@dcharkes
Copy link
Contributor

I believe it is similar to the issue described here.

It does sound like the same issue, the String memory being released on the native code side before the Dart callback is run (asynchronously).

Is there a workaround for this?

Yes, malloc the String in C and pass ownership to Dart.

I think the proposed NativeCallable.blocking might solve it but in the meantime is there anything I can do from the dart side?

It would (and would make the callback synchronous instead of asynchronous).

No, you cannot do anything from the Dart side.

You'll have to write a C function that copies the String into malloced memory and pass that copy of the string to Dart so that then in Dart you can free it when you're done with it.

@LeoBound
Copy link
Author

Ok I'll make the changes on the C side for now.

If I had a library for which I didn't have the source code (only binaries and headers) am I right in saying the only workaround for this at the moment to wrap that library in a layer that makes copies for all of the callbacks?

@dcharkes
Copy link
Contributor

If I had a library for which I didn't have the source code (only binaries and headers) am I right in saying the only workaround for this at the moment to wrap that library in a layer that makes copies for all of the callbacks?

Correct 👍

@LeoBound
Copy link
Author

Fair enough, thanks for getting back to me quickly! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-native-interop Used for native interop related issues, including FFI. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants