Don't close serial port before switching to state "closed serial port"

Depending on what was happening in the monitoring thread this could lead to an attempt to write to the already closed port, logging an error and killing the interface in the process. Also fixed a timing issue in the state reporting towards the frontend, state updates and readings for clients are now wrapped in a mutex

Closes #492
This commit is contained in:
Gina Häußge 2014-08-07 18:07:19 +02:00
parent c026d7d248
commit 11fb18f598
2 changed files with 15 additions and 12 deletions

View file

@ -688,6 +688,7 @@ class StateMonitor(object):
self._offsets = {} self._offsets = {}
self._changeEvent = threading.Event() self._changeEvent = threading.Event()
self._stateMutex = threading.Lock()
self._lastUpdate = time.time() self._lastUpdate = time.time()
self._worker = threading.Thread(target=self._work) self._worker = threading.Thread(target=self._work)
@ -717,6 +718,7 @@ class StateMonitor(object):
self._changeEvent.set() self._changeEvent.set()
def setState(self, state): def setState(self, state):
with self._stateMutex:
self._state = state self._state = state
self._changeEvent.set() self._changeEvent.set()
@ -736,6 +738,7 @@ class StateMonitor(object):
while True: while True:
self._changeEvent.wait() self._changeEvent.wait()
with self._stateMutex:
now = time.time() now = time.time()
delta = now - self._lastUpdate delta = now - self._lastUpdate
additionalWaitTime = self._ratelimit - delta additionalWaitTime = self._ratelimit - delta

View file

@ -340,11 +340,11 @@ class MachineCom(object):
def close(self, isError = False): def close(self, isError = False):
printing = self.isPrinting() or self.isPaused() printing = self.isPrinting() or self.isPaused()
if self._serial is not None: if self._serial is not None:
self._serial.close()
if isError: if isError:
self._changeState(self.STATE_CLOSED_WITH_ERROR) self._changeState(self.STATE_CLOSED_WITH_ERROR)
else: else:
self._changeState(self.STATE_CLOSED) self._changeState(self.STATE_CLOSED)
self._serial.close()
self._serial = None self._serial = None
if settings().get(["feature", "sdSupport"]): if settings().get(["feature", "sdSupport"]):