From b93b092dfb0364bc3a34cfeef34fab1a408ce369 Mon Sep 17 00:00:00 2001
From: Szymon Lesisz <sz.lesisz@gmail.com>
Date: Thu, 16 Apr 2020 20:40:16 +0200
Subject: [PATCH] fix trezord unexpected message loop (#561)

* fix trezord unexpected message loop

* Update DeviceCommands.js
---
 src/js/device/DeviceCommands.js | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/js/device/DeviceCommands.js b/src/js/device/DeviceCommands.js
index bfe9369ba..6ccb70237 100644
--- a/src/js/device/DeviceCommands.js
+++ b/src/js/device/DeviceCommands.js
@@ -635,7 +635,15 @@ export default class DeviceCommands {
         }
 
         const response: DefaultMessageResponse = await this._commonCall(type, msg);
-        assertType(response, resType);
+        try {
+            assertType(response, resType);
+        } catch (error) {
+            // handle possible race condition
+            // Bridge may have some unread message in buffer, read it
+            await this.transport.read(this.sessionId, false);
+            // throw error anyway, next call should be resolved properly
+            throw error;
+        }
         return response;
     }