Fix broken lock file for "appstate" storage // Resolve #288

This commit is contained in:
Ivan Kravets
2015-09-23 16:35:31 +03:00
parent ee7fe1fc10
commit 46d1c2c86c
3 changed files with 23 additions and 4 deletions

View File

@ -4,6 +4,13 @@ Release History
PlatformIO 2.0 PlatformIO 2.0
-------------- --------------
2.3.3 (2015-09-??)
~~~~~~~~~~~~~~~~~~
* Fixed broken lock file for "appstate" storage
(`issue #288 <https://github.com/platformio/platformio/issues/288>`_)
2.3.2 (2015-09-10) 2.3.2 (2015-09-10)
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

View File

@ -1,7 +1,7 @@
# Copyright (C) Ivan Kravets <me@ikravets.com> # Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details. # See LICENSE for details.
VERSION = (2, 3, 2) VERSION = (2, 3, "3.dev0")
__version__ = ".".join([str(s) for s in VERSION]) __version__ = ".".join([str(s) for s in VERSION])
__title__ = "platformio" __title__ = "platformio"

View File

@ -3,7 +3,8 @@
import json import json
from os import environ, getenv from os import environ, getenv
from os.path import isfile, join from os.path import getmtime, isfile, join
from time import time
from lockfile import LockFile from lockfile import LockFile
@ -68,9 +69,8 @@ class State(object):
def __enter__(self): def __enter__(self):
try: try:
self._lock_state_file()
if isfile(self.path): if isfile(self.path):
self._lock = LockFile(self.path)
self._lock.acquire()
with open(self.path, "r") as fp: with open(self.path, "r") as fp:
self._state = json.load(fp) self._state = json.load(fp)
except ValueError: except ValueError:
@ -85,6 +85,18 @@ class State(object):
json.dump(self._state, fp, indent=4) json.dump(self._state, fp, indent=4)
else: else:
json.dump(self._state, fp) json.dump(self._state, fp)
self._unlock_state_file()
def _lock_state_file(self):
self._lock = LockFile(self.path)
if (self._lock.is_locked() and
(time() - getmtime(self._lock.lock_file)) > 10):
self._lock.break_lock()
self._lock.acquire()
def _unlock_state_file(self):
if self._lock: if self._lock:
self._lock.release() self._lock.release()