optuna: `JSONDecodeError` in `JournalStorage`
Expected behavior
The all CIs should be passed but test_pop_waiting_trial_multiprocess_safe in tests/storages_tests/test_journal.py sometimes fails. https://github.com/optuna/optuna/actions/runs/7079914046/job/19267103786
Environment
- Optuna version: master
- Python version: (at least) 3.7 & 3.8
- OS: Ubuntu
- (Optional) Other libraries and their versions:
Error messages, stack traces, or logs
___________________ test_pop_waiting_trial_multiprocess_safe ___________________
concurrent.futures.process._RemoteTraceback:
"""
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/concurrent/futures/process.py", line 239, in _process_worker
r = call_item.fn(*call_item.args, **call_item.kwargs)
File "/home/runner/work/optuna/optuna/tests/storages_tests/test_journal.py", line 103, in pop_waiting_trial
study = optuna.load_study(storage=storage, study_name=study_name)
File "/home/runner/work/optuna/optuna/optuna/_convert_positional_args.py", line 83, in converter_wrapper
return func(**kwargs)
File "/home/runner/work/optuna/optuna/optuna/study/study.py", line 1350, in load_study
return Study(study_name=study_name, storage=storage, sampler=sampler, pruner=pruner)
File "/home/runner/work/optuna/optuna/optuna/study/study.py", line 85, in __init__
self._directions = storage.get_study_directions(study_id)
File "/home/runner/work/optuna/optuna/optuna/storages/_journal/storage.py", line 213, in get_study_directions
self._sync_with_backend()
File "/home/runner/work/optuna/optuna/optuna/storages/_journal/storage.py", line 149, in _sync_with_backend
logs = self._backend.read_logs(self._replay_result.log_number_read)
File "/home/runner/work/optuna/optuna/optuna/storages/_journal/file.py", line 175, in read_logs
raise last_decode_error
File "/home/runner/work/optuna/optuna/optuna/storages/_journal/file.py", line 184, in read_logs
logs.append(json.loads(line))
File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 146 (char 145)
"""
The above exception was the direct cause of the following exception:
def test_pop_waiting_trial_multiprocess_safe() -> None:
with NamedTemporaryFilePool() as file:
file_storage = optuna.storages.JournalFileStorage(file.name)
storage = optuna.storages.JournalStorage(file_storage)
study = optuna.create_study(storage=storage)
num_enqueued = 10
for i in range(num_enqueued):
study.enqueue_trial({"i": i})
trial_id_set = set()
with ProcessPoolExecutor(10) as pool:
futures = []
for i in range(num_enqueued):
future = pool.submit(pop_waiting_trial, file.name, study.study_name)
futures.append(future)
for future in as_completed(futures):
> trial_id = future.result()
tests/storages_tests/test_journal.py:124:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/concurrent/futures/_base.py:437: in result
return self.__get_result()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = None
def __get_result(self):
if self._exception:
try:
> raise self._exception
E json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 146 (char 145)
/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/concurrent/futures/_base.py:389: JSONDecodeError
Steps to reproduce
Run test_pop_waiting_trial_multiprocess_safe in tests/storages_tests/test_journal.py.
Additional context (optional)
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 24 (12 by maintainers)
Commits related to this issue
- Fix JSONDecodeError in JournalStorage #5138 — committed to y0z/optuna by y0z 3 years ago
Okay, turns our the file system is something called juicefs.
#5195 fixed this error. It will be included in the next minor release. Thank you for your reports and contributions!
Thank you for your reports. I investigated this bug and found that it is caused by the wrong reading log logic. We plan to fix it until the next minor release.
It’s the latest. Here is the docker image.
I deleted the corrupted log file generated earlier. However, when I was trying to reproduce the problem, a different error showed up:
I started two processes, the first one stoped due to the error while the second one seem to run just fine. Here is the log file generated by optuna:
Note that there seems to be an empty line at the end of the file, which could be the problem here.
Yes of course:) The error looks like similar to the one reported earlier. It seems that the trial can start running for a while until another process started can corrupt the log file.
By the way here’s the version just to confirm: