From 1eb0738a72807b2690e97c8e185cea5dfa55a5d4 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sun, 17 May 2020 13:52:35 +0200 Subject: [PATCH] fix crash with HTTP finished signal being called also in error states --- CHANGELOG.md | 3 ++ DuetRRFOutputDevice.py | 70 ++++++++++++++++++++++++++---------------- plugin.json | 2 +- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06c4c36..c70bf8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog of Cura-DuetRRFPlugin +## v1.0.10: 2020-05-XX + * fix Cura crashes when using Duet3 with SBC + ## v1.0.9: 2020-05-02 * bump compatibility for Cura 4.6 / API 7.2 * fix simulation result reply for RRF HTTP API diff --git a/DuetRRFOutputDevice.py b/DuetRRFOutputDevice.py index 7b6ff33..bd26b24 100644 --- a/DuetRRFOutputDevice.py +++ b/DuetRRFOutputDevice.py @@ -63,6 +63,7 @@ def __init__(self, name, url, duet_password, http_user, http_password, device_ty self.setPriority(priority) self._stage = OutputStage.ready + self._stream = None self._name = name self._name_id = name_id self._device_type = device_type @@ -79,9 +80,6 @@ def __init__(self, name, url, duet_password, http_user, http_password, device_ty Logger.log("d", self._name_id + " | HTTP Basic Auth user: " + ("set." if self._http_user else "empty.")) Logger.log("d", self._name_id + " | HTTP Basic Auth password: " + ("set." if self._http_password else "empty.")) - self._qnam = QtNetwork.QNetworkAccessManager() - - self._stream = None self._cleanupRequest() if hasattr(self, '_message'): @@ -101,26 +99,27 @@ def _send(self, command, query=None, next_stage=None, data=None, on_error=None, if enc_query: url += '?' + enc_query - self._request = QtNetwork.QNetworkRequest(QUrl(url)) - self._request.setRawHeader(b'User-Agent', b'Cura Plugin DuetRRF') - self._request.setRawHeader(b'Accept', b'application/json, text/javascript') - self._request.setRawHeader(b'Connection', b'keep-alive') + request = QtNetwork.QNetworkRequest(QUrl(url)) + request.setRawHeader(b'User-Agent', b'Cura Plugin DuetRRF') + request.setRawHeader(b'Accept', b'application/json, text/javascript') + request.setRawHeader(b'Connection', b'keep-alive') if self._http_user and self._http_password: - self._request.setRawHeader(b'Authorization', b'Basic ' + base64.b64encode("{}:{}".format(self._http_user, self._http_password).encode())) + request.setRawHeader(b'Authorization', b'Basic ' + base64.b64encode("{}:{}".format(self._http_user, self._http_password).encode())) + + self._qnam = QtNetwork.QNetworkAccessManager() + if next_stage: + self._qnam.finished.connect(next_stage) if data: - self._request.setRawHeader(b'Content-Type', b'application/octet-stream') + request.setRawHeader(b'Content-Type', b'application/octet-stream') if method == 'PUT': - self._reply = self._qnam.put(self._request, data) + self._reply = self._qnam.put(request, data) else: - self._reply = self._qnam.post(self._request, data) + self._reply = self._qnam.post(request, data) self._reply.uploadProgress.connect(self._onUploadProgress) else: - self._reply = self._qnam.get(self._request) - - if next_stage: - self._reply.finished.connect(next_stage) + self._reply = self._qnam.get(request) if on_error: self._reply.error.connect(on_error) @@ -210,16 +209,15 @@ def _check_duet3_sbc(self, errorCode): else: self._onNetworkError(errorCode) - def onUploadReady(self): + def onUploadReady(self, reply): if self._stage != OutputStage.writing: return + if reply.error() != QNetworkReply.NoError: + Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error()) + return Logger.log("d", self._name_id + " | Uploading...") - if not self._stream: - Logger.log("d", self._name_id + " | Upload failed because stream is already None - THIS SHOULD NOT HAPPEN!") - return - self._stream.seek(0) self._postData = QByteArray() self._postData.append(self._stream.getvalue().encode()) @@ -237,9 +235,12 @@ def onUploadReady(self): method='PUT', ) - def onUploadDone(self): + def onUploadDone(self, reply): if self._stage != OutputStage.writing: return + if reply.error() != QNetworkReply.NoError: + Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error()) + return Logger.log("d", self._name_id + " | Upload done") @@ -300,9 +301,12 @@ def onReadyToPrint(self): next_stage=self.onPrintStarted, ) - def onPrintStarted(self): + def onPrintStarted(self, reply): if self._stage != OutputStage.writing: return + if reply.error() != QNetworkReply.NoError: + Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error()) + return Logger.log("d", self._name_id + " | Print started") @@ -319,9 +323,12 @@ def onPrintStarted(self): self.writeSuccess.emit(self) self._cleanupRequest() - def onSimulationPrintStarted(self): + def onSimulationPrintStarted(self, reply): if self._stage != OutputStage.writing: return + if reply.error() != QNetworkReply.NoError: + Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error()) + return Logger.log("d", self._name_id + " | Simulation print started for file " + self._fileName) @@ -344,9 +351,12 @@ def onCheckStatus(self): next_stage=self.onStatusReceived, ) - def onStatusReceived(self): + def onStatusReceived(self, reply): if self._stage != OutputStage.writing: return + if reply.error() != QNetworkReply.NoError: + Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error()) + return Logger.log("d", self._name_id + " | Status received - decoding...") reply_body = bytes(self._reply.readAll()).decode() @@ -380,9 +390,12 @@ def onStatusReceived(self): data=gcode.encode(), next_stage=self.onReported, ) - def onM37Reported(self): + def onM37Reported(self, reply): if self._stage != OutputStage.writing: return + if reply.error() != QNetworkReply.NoError: + Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error()) + return Logger.log("d", self._name_id + " | M37 finished - let's get it's reply...") reply_body = bytes(self._reply.readAll()).decode().strip() @@ -392,9 +405,12 @@ def onM37Reported(self): next_stage=self.onReported, ) - def onReported(self): + def onReported(self, reply): if self._stage != OutputStage.writing: return + if reply.error() != QNetworkReply.NoError: + Logger.log("d", self._name_id + " | Stopping due to reply error: " + reply.error()) + return Logger.log("d", self._name_id + " | Simulation status received - decoding...") reply_body = bytes(self._reply.readAll()).decode().strip() @@ -421,8 +437,8 @@ def _onProgress(self, progress): def _cleanupRequest(self): Logger.log("e", "_cleanupRequest called") + self._qnam = None self._reply = None - self._request = None if self._stream: self._stream.close() self._stream = None diff --git a/plugin.json b/plugin.json index a5789bf..297f082 100644 --- a/plugin.json +++ b/plugin.json @@ -2,6 +2,6 @@ "name": "DuetRRF", "author": "Thomas Kriechbaumer", "description": "Upload and Print to Duet 2 Wifi / Duet 2 Ethernet / Duet 2 Maestro / Duet 3 with RepRapFirmware.", - "version": "1.0.9", + "version": "1.0.10", "supported_sdk_versions": ["7.2.0"] }