audio_waveforms icon indicating copy to clipboard operation
audio_waveforms copied to clipboard

fix: Android recorderController.stop() never completes

Open rubenvde opened this issue 8 months ago • 4 comments

Description

Fixes a hang where await recorderController.stop() on Android never completes, especially for recordings >30 seconds. This was introduced in the 2.0.x rewrite that replaced MediaRecorder with AudioRecord + MediaCodec.

Root causes fixed:

  1. Deadlock in stopEncoder() — stopEncoder() is called from onOutputBufferAvailable on the handler thread, but it called handlerThread.join(), waiting for itself to finish. Removed the join() and use quitSafely() only.

  2. Race condition in signalToStop() — EOS buffer queuing now runs on the handler thread via handler.post {}, avoiding races with onInputBufferAvailable that caused "MediaCodec discarded an unknown buffer" errors.

  3. release() called before async completion — For the non-WAV path, release() was called synchronously after signalToStop(), before the completion callback fired. Moved release() into the completion callback.

  4. result.success() called from wrong thread — sendRecordingResult() now posts to the main looper, since Flutter's MethodChannel.Result must be called on the platform thread.

  5. completionCallback swallowed on error — Moved completionCallback?.invoke() to a finally block in stopEncoder() so the Flutter result is always resolved.

  6. Missing handler in setCallback() — mediaCodec.setCallback() now passes the handler so callbacks run on the intended HandlerThread.

Checklist

  • [x] The title of my PR starts with a [Conventional Commit] prefix (fix:, feat:, docs: etc).
  • [x] I have followed the [Contributor Guide] when preparing my PR.
  • [ ] I have updated/added tests for ALL new/updated/fixed functionality.
  • [x] I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [x] I have updated/added relevant examples in examples or docs.

There's no test infrastructure in the project and the bug is a native Android threading issue that's difficult to unit test from Dart

Breaking Change?

  • [ ] Yes, this PR is a breaking change.
  • [x] No, this PR is not a breaking change.

Related Issues

#470

rubenvde avatar Apr 03 '26 11:04 rubenvde

Same issue!

_playerControler?.dispose();

yuanhoujun avatar Apr 04 '26 15:04 yuanhoujun

This does not work for me it actually crashes the app when I call stop().

DorraY avatar Apr 13 '26 07:04 DorraY

Thanks for the fix, @rubenvde! This solves the stop()-never-completing hang nicely.

I've left a few comments on the diff, a couple worth addressing before merge, plus two minor cleanups. Could you take a look and resolve them? Happy to discuss any of them. Thanks again!

lavigarg-simform avatar Jun 23 '26 13:06 lavigarg-simform

Same issue!

_playerControler?.dispose();

Hi @yuanhoujun! Thanks for chiming in. Just a heads-up: this PR is about the recorder- RecorderController.stop() never completing. Your snippet uses PlayerController.dispose(), which is a different subsystem (audio playback), so this fix won't address it. Could you please check if PR #495 solves your issue? Also worth checking issues #477, #478. If none of the issues resonate with your bug, could you open a separate issue for the player-dispose problem with a stack trace and repro steps?

lavigarg-simform avatar Jun 23 '26 14:06 lavigarg-simform

@lavigarg-simform We've verified that the core issue this PR addresses is real and matches our production findings. On Galaxy Tab A9 (Android 14), stop() deadlocks 100% for recordings ≥30s — consistent with the self-join deadlock in stopEncoder().

We confirmed through debugging that the hang occurs specifically in the native stop() call, and also in preparePlayer() which shares the same threading model. The 6 threading issues identified here are well-analyzed and the fixes are sound.

Would appreciate this getting merged when the minor feedback is resolved — we currently have to discard recordings on timeout as a workaround.

junddao avatar Jul 06 '26 06:07 junddao

@lavigarg-simform Are you no longer involved with this library?

junddao avatar Jul 29 '26 07:07 junddao