Skip to content

Commit

Permalink
Fix broken lock file for "appstate" storage // Resolve #288
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankravets committed Sep 23, 2015
1 parent ee7fe1f commit 46d1c2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
7 changes: 7 additions & 0 deletions HISTORY.rst
Expand Up @@ -4,6 +4,13 @@ Release History
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)
~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion platformio/__init__.py
@@ -1,7 +1,7 @@
# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.

VERSION = (2, 3, 2)
VERSION = (2, 3, "3.dev0")
__version__ = ".".join([str(s) for s in VERSION])

__title__ = "platformio"
Expand Down
18 changes: 15 additions & 3 deletions platformio/app.py
Expand Up @@ -3,7 +3,8 @@

import json
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

Expand Down Expand Up @@ -68,9 +69,8 @@ def __init__(self, path=None):

def __enter__(self):
try:
self._lock_state_file()
if isfile(self.path):
self._lock = LockFile(self.path)
self._lock.acquire()
with open(self.path, "r") as fp:
self._state = json.load(fp)
except ValueError:
Expand All @@ -85,6 +85,18 @@ def __exit__(self, type_, value, traceback):
json.dump(self._state, fp, indent=4)
else:
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:
self._lock.release()

Expand Down

0 comments on commit 46d1c2c

Please sign in to comment.