IllegalStateException: Surface has been abandoned while recording

Hello,
I’m developing an app that requires to record camera to mp4 file.
I’m using Camera2 API and MediaRecorder with ImageReader and ImageWriter.
The problem is after some short period of time recording stops with an exception:

attachBuffer: IGraphicBufferProducer call failed (-19)
ImageWriter_JNI com.example.camera2test E Attach image failed: No such device (-19)
IllegalStateException: Surface has been abandoned

The issue is only reproduceble on SM-G955U1 and works perfectly on other devices.
Is there any workaround for this?