Skip to content

Commit 86cca01

Browse files
committed
fix(react): guard processor cast and detach on unmount
Two correctness fixes from PR review: - Guard the `KrispNoiseFilterProcessor` cast in the returned `processor` value with the same `name === 'livekit-noise-filter'` check used inside the effect. The unconditional cast assumed nothing else could attach a `TrackProcessor` to the track; if anything did, the hook handed consumers a value typed as a Krisp processor that wasn't one. - Add an unmount-only effect that detaches the Krisp processor from the currently-managed track. Empty deps so it doesn't interfere with the existing toggle-reuse path on shouldEnable / filterOptions / track changes. Closes the gap where a caller-owned `trackRef` outlives the hook's component, leaving the track with a Krisp processor attached that the hook no longer manages.
1 parent 63fa161 commit 86cca01

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

packages/react/src/hooks/cloud/krisp/useKrispNoiseFilter.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export function useKrispNoiseFilter(options: useKrispNoiseFilterOptions = {}) {
4848
micPublication = options.trackRef.publication;
4949
}
5050

51+
const managedTrackRef = React.useRef<LocalAudioTrack | undefined>(undefined);
52+
managedTrackRef.current =
53+
micPublication?.track instanceof LocalAudioTrack ? micPublication.track : undefined;
54+
5155
const setNoiseFilterEnabled = React.useCallback(async (enable: boolean) => {
5256
if (enable) {
5357
const { isKrispNoiseFilterSupported } = await import('@livekit/krisp-noise-filter');
@@ -126,9 +130,24 @@ export function useKrispNoiseFilter(options: useKrispNoiseFilterOptions = {}) {
126130
};
127131
}, [shouldEnable, micPublication?.track, options.filterOptions]);
128132

129-
const processor =
133+
React.useEffect(() => {
134+
return () => {
135+
const track = managedTrackRef.current;
136+
if (!track) return;
137+
if (track.getProcessor()?.name !== 'livekit-noise-filter') return;
138+
track.stopProcessor().catch((e) => {
139+
log.warn('Krisp hook: error detaching processor on unmount', e);
140+
});
141+
};
142+
}, []);
143+
144+
const trackProcessor =
130145
micPublication?.track instanceof LocalAudioTrack
131-
? (micPublication.track.getProcessor() as KrispNoiseFilterProcessor | undefined)
146+
? micPublication.track.getProcessor()
147+
: undefined;
148+
const processor =
149+
trackProcessor?.name === 'livekit-noise-filter'
150+
? (trackProcessor as KrispNoiseFilterProcessor)
132151
: undefined;
133152

134153
return {

0 commit comments

Comments
 (0)