[ML] @kbn/ml-response-stream: Fix race condition related to throttling. (#162803)

Fixes a race condition in the case where a response stream finishes and
sets `isRunning` to `false`, but `useThrottle` didn't trigger it's last
update yet within the refresh rate. In the case of log rate analysis,
`isRunning` could be set to `false` too early and the UI wouldn't
consider later throttled updates (for example, setting `loaded=1` which
would result in inconsistent UI state).

The fix in this case is to return the unthrottled raw data instead of
the throttled one as soon as the stream finished.
This commit is contained in:
Walter Rafelsberger 2023-07-31 17:39:25 +02:00 committed by GitHub
parent f0050dbc70
commit 96de1482e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -129,7 +129,10 @@ export function useFetchStream<B extends object, R extends Reducer<any, any>>(
return {
cancel,
data: dataThrottled,
// To avoid a race condition where the stream already ended but `useThrottle` would
// yet have to trigger another update within the throttling interval, we'll return
// the unthrottled data once the stream is complete.
data: isRunning ? dataThrottled : data,
dispatch,
errors,
isCancelled,