fix: add inference mutex to Text Embedding and Text-to-Image#1060
Merged
msluszniak merged 5 commits intomainfrom Apr 10, 2026
Merged
fix: add inference mutex to Text Embedding and Text-to-Image#1060msluszniak merged 5 commits intomainfrom
msluszniak merged 5 commits intomainfrom
Conversation
msluszniak
approved these changes
Apr 8, 2026
Member
There was a problem hiding this comment.
With fix, I indeed got segfault on provided demo app code:
04-08 13:39:15.046 5901 5994 F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x7844af9aec in tid 5994 (RN_ET_Worker2), pid 5901 (com.barern)
04-08 13:39:15.542 6059 6059 F DEBUG : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0000007844af9aec
when applied changes from this PR, no crashes occur.
chmjkb
reviewed
Apr 9, 2026
Collaborator
chmjkb
left a comment
There was a problem hiding this comment.
Other than this I think it looks good.
packages/react-native-executorch/common/rnexecutorch/host_objects/ModelHostObject.h
Show resolved
Hide resolved
chmjkb
approved these changes
Apr 10, 2026
msluszniak
pushed a commit
that referenced
this pull request
Apr 10, 2026
## Description
Adds thread-safety to Text Embeddings and Text-to-Image models mirroring
what was already done for models inheriting from VisionModel and VAD.
### Introduces a breaking change?
- [ ] Yes
- [x] No
### Type of change
- [x] Bug fix (change which fixes an issue)
- [ ] New feature (change which adds functionality)
- [ ] Documentation update (improves or adds clarity to existing
documentation)
- [ ] Other (chores, tests, code style improvements etc.)
### Tested on
- [x] iOS
- [x] Android
### Testing instructions
Use the following app screen and try to trigger the race condition
before fix and verify that it doesn't occur after applying the fix. You
can use `adb logcat | grep -E "FATAL|SIGSEGV|backtrace"` to observe the
error on Android.
```ts
import React, { useState } from 'react';
import { Button, ScrollView, Text, View } from 'react-native';
import {
BK_SDM_TINY_VPRED_512,
TextToImageModule,
} from 'react-native-executorch';
import {
CLIP_VIT_BASE_PATCH32_TEXT,
TextEmbeddingsModule,
} from 'react-native-executorch';
import { FSMN_VAD, VADModule } from 'react-native-executorch';
const DELAY_MS = 50; // tune this so that forward() is running when delete() is called
const MODEL_VAD = {
name: 'VAD',
load: (onProgress: (p: number) => void) =>
VADModule.fromModelName(FSMN_VAD, onProgress),
input: () => new Float32Array(16000 * 300),
};
const MODEL_TEXT_EMBEDDINGS = {
name: 'TextEmbeddings',
load: (onProgress: (p: number) => void) =>
TextEmbeddingsModule.fromModelName(CLIP_VIT_BASE_PATCH32_TEXT, onProgress),
input: () => 'hello world',
};
const MODEL_TEXT_TO_IMAGE = {
name: 'TextToImage',
load: (onProgress: (p: number) => void) =>
TextToImageModule.fromModelName(BK_SDM_TINY_VPRED_512, onProgress),
input: () => 'a red apple',
};
const MODEL = MODEL_TEXT_EMBEDDINGS;
export default function RaceTest() {
const [lines, setLines] = useState<string[]>([]);
const [downloadProgress, setDownloadProgress] = useState<number | null>(null);
const log = (line: string) => setLines((prev) => [line, ...prev]);
const run = async () => {
setLines([]);
setDownloadProgress(null);
log(`model: ${MODEL.name}`);
log('loading');
const model = await MODEL.load((p) => setDownloadProgress(p));
setDownloadProgress(null);
log('running forward()');
const result = model.forward(MODEL.input());
log(`waiting ${DELAY_MS} ms`);
await new Promise((r) => setTimeout(r, DELAY_MS));
log('calling delete()');
model.delete();
try {
await result;
log('forward() completed successfully');
} catch (e: any) {
log('error: ' + (e?.message ?? String(e)));
}
};
return (
<View>
<Button title="Run Race Test" onPress={run} />
{downloadProgress !== null && (
<Text>downloading: {Math.round(downloadProgress * 100)}%</Text>
)}
<ScrollView>
{lines.map((l, i) => (
<Text key={i}>{l}</Text>
))}
</ScrollView>
</View>
);
}
```
### Screenshots
<!-- Add screenshots here, if applicable -->
### Related issues
#1055
### Checklist
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have updated the documentation accordingly
- [x] My changes generate no new warnings
### Additional notes
<!-- Include any additional information, assumptions, or context that
reviewers might need to understand this PR. -->
msluszniak
added a commit
that referenced
this pull request
Apr 10, 2026
## Summary Patch release v0.8.3 — cherry-picks the following bug fixes from `main` into `release/0.8`: - fix: add mutex to VoiceActivityDetection to prevent race between `generate()` and `unload()` (#1056) - fix: prevent apps from crashing when LLMs are loaded (#1063) - fix: add inference mutex to Text Embedding and Text-to-Image (#1060) ## Checklist - [x] Commits cherry-picked from `main` in chronological order - [x] Version bumped to `0.8.3` in `packages/react-native-executorch/package.json` 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Radek Czemerys <7029942+radko93@users.noreply.github.com> Co-authored-by: Bartosz Hanc <bartosz.hanc02@gmail.com> Co-authored-by: Jakub Chmura <92989966+chmjkb@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds thread-safety to Text Embeddings and Text-to-Image models mirroring what was already done for models inheriting from VisionModel and VAD.
Introduces a breaking change?
Type of change
Tested on
Testing instructions
Use the following app screen and try to trigger the race condition before fix and verify that it doesn't occur after applying the fix. You can use
adb logcat | grep -E "FATAL|SIGSEGV|backtrace"to observe the error on Android.Screenshots
Related issues
#1055
Checklist
Additional notes