Skip to content

contest: retry when unable to load input files - #90

Merged
kuba-moo merged 1 commit into
linux-netdev:mainfrom
matttbe:contest-retry-empty
Aug 27, 2026
Merged

contest: retry when unable to load input files#90
kuba-moo merged 1 commit into
linux-netdev:mainfrom
matttbe:contest-retry-empty

Conversation

@matttbe

@matttbe matttbe commented Aug 20, 2026

Copy link
Copy Markdown
Member

The contest service was unable to load the 'branch_info' input file twice yesterday. Probably because it was being updated:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Catch the error, and retry max 4 more times with the same delay.

@matttbe
matttbe requested a review from kuba-moo August 20, 2026 10:30
@kuba-moo

Copy link
Copy Markdown
Contributor

I thought we had some "atomic update" method for updating the JSON files. Does it still race? Maybe some files are missing using that atomic update API?

@matttbe

matttbe commented Aug 23, 2026

Copy link
Copy Markdown
Member Author

From what I understood, nipa-br-faker.service is executed every 2 minutes, and it combines branches-info-nn.json and branches-info-hw.json into branches-info.json used by the "contest" service. The faker service reads the 2 json, combines them, opens the output file, truncating the file first, then writes it and closes it:

def combine_infos(config):
paths = config.get("input", "infos", fallback="").split(',')
if not paths:
return
infos = {}
for path in paths:
with open(path, "r") as fp:
infos.update(json.load(fp))
with open(config.get("output", "info"), 'w') as fp:
json.dump(infos, fp)

It looks like there is a very small window where the file can be empty.

@kuba-moo

Copy link
Copy Markdown
Contributor

That's the one, it should be doing an "atomic" write, write to a temp file and rename it overriding the existing file, instead of dumping directly to the output.

@matttbe
matttbe force-pushed the contest-retry-empty branch from d8171b9 to 73c9215 Compare August 27, 2026 10:56
The contest service was unable to load the 'branch_info' input file
twice recently. Probably because it was being updated:

  json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Instead of catching the error and retry later, this file can be written
atomically by using a tmp file and renaming it to the expected name.
That's the recommended way [1]:

  os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None):

    (...)

    If both are files, dst will be replaced silently if the user has
    permission. (...) If successful, the renaming will be an atomic
    operation (this is a POSIX requirement).

Link: https://docs.python.org/3/library/os.html#os.rename [1]
Signed-off-by: Matthieu Baerts <matttbe@kernel.org>
@matttbe
matttbe force-pushed the contest-retry-empty branch from 73c9215 to 8f32c6c Compare August 27, 2026 10:58
@matttbe

matttbe commented Aug 27, 2026

Copy link
Copy Markdown
Member Author

That's the one, it should be doing an "atomic" write, write to a temp file and rename it overriding the existing file, instead of dumping directly to the output.

Indeed, better with an atomic write! I just fixed that.

@kuba-moo
kuba-moo merged commit c7f0b80 into linux-netdev:main Aug 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants