Skip to content

Commit 961aef0

Browse files
[tsmeta.bbclass] Add lockfiles when writing and reading from intermediate json file to avoid race conditions
There are multiple tasks which generate intermediate Json files. A race condition occurs if the same file is being read by a task while its still being edited. To fix this condition bitbake's file locking mechanism is used in `tsmeta_read_json` and `tsmeta_write_json` functions to lock a file being accessed by a task until that task is completed
1 parent 059dfcb commit 961aef0

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

classes/tsmeta.bbclass

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,25 @@ def tsmeta_read_json(d, trj_path):
108108
import json
109109
dict_in = dict()
110110
if os.path.exists(trj_path):
111-
with open(trj_path) as f:
112-
dict_in = json.load(f)
111+
lock = bb.utils.lockfile(trj_path + ".lock")
112+
try:
113+
with open(trj_path) as f:
114+
dict_in = json.load(f)
115+
finally:
116+
bb.utils.unlockfile(lock)
113117
return dict_in
114118

115119
def tsmeta_write_json(d, dict_out, twj_path):
116120
import json
117121

118122
s = json.dumps(dict_out, indent=8, sort_keys=False)
119123
if twj_path:
120-
with open(twj_path, "w") as f:
121-
f.write(s)
124+
lock = bb.utils.lockfile(twj_path + ".lock")
125+
try:
126+
with open(twj_path, "w") as f:
127+
f.write(s)
128+
finally:
129+
bb.utils.unlockfile(lock)
122130

123131

124132
def tsmeta_write_dictname(d, tsm_type, twd_name, twd_dict):
@@ -479,7 +487,6 @@ python do_tsmeta_pkgvars() {
479487
tsmeta_get_src(d)
480488
tsmeta_get_pkg(d)
481489
}
482-
do_tsmeta_pkgvars[lockfiles] = "${tsmeta_dir}/tsmeta_lockfile"
483490

484491

485492
def tsmeta_collect_preferred(d):

0 commit comments

Comments
 (0)