@@ -7,11 +7,13 @@ import ai.openclaw.app.takeUtf16Safe
77import android.content.Context
88import kotlinx.coroutines.CancellationException
99import kotlinx.coroutines.Dispatchers
10+ import kotlinx.coroutines.NonCancellable
1011import kotlinx.coroutines.withContext
1112import kotlinx.serialization.json.JsonPrimitive
1213import kotlinx.serialization.json.buildJsonArray
1314import kotlinx.serialization.json.buildJsonObject
1415import kotlinx.serialization.json.put
16+ import java.util.concurrent.atomic.AtomicReference
1517
1618internal const val CAMERA_CLIP_MAX_RAW_BYTES : Long = 18L * 1024L * 1024L
1719private const val CAMERA_DEBUG_STACK_TRACE_MAX_CHARS = 2_000
@@ -121,6 +123,7 @@ class CameraHandler(
121123 message = " MIC_BUSY: another audio capture is active" ,
122124 )
123125 }
126+ val ownedClipFile = AtomicReference < java.io.File ? > ()
124127 try {
125128 clipLogFile?.writeText(" " ) // clear
126129 clipLog(" starting, params=$paramsJson includeAudio=$includeAudio " )
@@ -129,7 +132,10 @@ class CameraHandler(
129132 val filePayload =
130133 try {
131134 clipLog(" calling camera.clip()" )
132- val r = camera.clip(paramsJson)
135+ val r =
136+ camera.clip(paramsJson) { file ->
137+ check(ownedClipFile.compareAndSet(null , file)) { " camera clip already owns a file" }
138+ }
133139 clipLog(" success, file size=${r.file.length()} " )
134140 r
135141 } catch (err: CancellationException ) {
@@ -144,8 +150,6 @@ class CameraHandler(
144150 val rawBytes = filePayload.file.length()
145151 if (! isCameraClipWithinPayloadLimit(rawBytes)) {
146152 clipLog(" payload too large: bytes=$rawBytes max=$CAMERA_CLIP_MAX_RAW_BYTES " )
147- // Delete oversized clips before returning so cache files do not accumulate after failed invokes.
148- withContext(Dispatchers .IO ) { filePayload.file.delete() }
149153 showCameraHud(" Clip too large" , CameraHudKind .Error , 2400 )
150154 return GatewaySession .InvokeResult .error(
151155 code = " PAYLOAD_TOO_LARGE" ,
@@ -154,14 +158,7 @@ class CameraHandler(
154158 )
155159 }
156160
157- val bytes =
158- withContext(Dispatchers .IO ) {
159- try {
160- filePayload.file.readBytes()
161- } finally {
162- filePayload.file.delete()
163- }
164- }
161+ val bytes = withContext(Dispatchers .IO ) { filePayload.file.readBytes() }
165162 val base64 = android.util.Base64 .encodeToString(bytes, android.util.Base64 .NO_WRAP )
166163 clipLog(" returning base64 payload" )
167164 showCameraHud(" Clip captured" , CameraHudKind .Success , 1800 )
@@ -175,8 +172,17 @@ class CameraHandler(
175172 clipLog(" stack: ${err.stackTraceToString().takeUtf16Safe(CAMERA_DEBUG_STACK_TRACE_MAX_CHARS )} " )
176173 return GatewaySession .InvokeResult .error(code = " UNAVAILABLE" , message = err.message ? : " camera clip failed" )
177174 } finally {
178- // Prevent talk/transcription capture from competing with camera audio after every exit path.
179- if (ownsAudioCapture) setCameraAudioCaptureActive(false )
175+ try {
176+ ownedClipFile.getAndSet(null )?.let { file ->
177+ // Nest dispatcher changes so cancellation cannot replace the original failure.
178+ withContext(NonCancellable ) {
179+ withContext(Dispatchers .IO ) { file.delete() }
180+ }
181+ }
182+ } finally {
183+ // Prevent talk/transcription capture from competing with camera audio after every exit path.
184+ if (ownsAudioCapture) setCameraAudioCaptureActive(false )
185+ }
180186 }
181187 }
182188
0 commit comments