Search code examples
python-3.xyamlspecial-charactersfile-writingnul

YAML file one line filled with null characters, #0000 character not supported while reading


I've built a python based application(which runs 24/7) that logs some information in a YAML file every few minutes. It was working perfectly for a few days. Suddenly after approximately after 2 weeks, one line in the YAML file was filled with NUL characters (416 characters of NUL to be precise).

Now the suspicion is that someone might've tried to open the already running application again, so both the applications tried to write/access the same YAML file which could've caused this. But I couldn't replicate this.

Just wanted to know the cause of this issue.

Please let me know if someone faced the same issue before.

Some context about the file writing: The YAML file will be loaded in append mode and a list is written inside it using the command below:

with open(file_path, 'a') as file:
    yaml.dump(summary_list, file)

Solution

  • Concurrent access is a possible cause for this especially when you're appending. For example, it may be that both instances opened the file and set the start marker on the same position, but let the file grow to the sum of both appended data dumps. That cause some part of the file not to be written, which might explain the NULs.

    Whatever happened is more dependent on your OS and your filesystem than it is on YAML. But even if we knew that we couldn't tell for sure.

    I recommend using a proper logging framework to avoid such issues; you can dump YAML as string to log it.