Imre Kis | a465cf4 | 2024-04-11 14:30:31 +0200 | [diff] [blame] | 1 | From 388b2d044d3cdab89b7e5e6de94436ca78e1a13b Mon Sep 17 00:00:00 2001 |
| 2 | From: Imre Kis <imre.kis@arm.com> |
| 3 | Date: Thu, 11 Apr 2024 14:20:16 +0200 |
| 4 | Subject: [PATCH] Fix race condition in directory creation |
| 5 | |
| 6 | nanopb_generator.py tests if the output directory exists and creates it |
| 7 | if not. Because of these two steps are not atomic, there's a chance of |
| 8 | multiple processes trying to create the directory. One process will |
| 9 | succeed but the other will fail because the directory already exists. |
| 10 | Allow existing directory on makedirs call to prevent this issue. |
| 11 | |
| 12 | Signed-off-by: Imre Kis <imre.kis@arm.com> |
| 13 | --- |
| 14 | generator/nanopb_generator.py | 2 +- |
| 15 | 1 file changed, 1 insertion(+), 1 deletion(-) |
| 16 | |
| 17 | diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py |
| 18 | index c11f6fa..999aaf7 100755 |
| 19 | --- a/generator/nanopb_generator.py |
| 20 | +++ b/generator/nanopb_generator.py |
| 21 | @@ -2597,7 +2597,7 @@ def main_cli(): |
| 22 | for path, data in to_write: |
| 23 | dirname = os.path.dirname(path) |
| 24 | if dirname and not os.path.exists(dirname): |
| 25 | - os.makedirs(dirname) |
| 26 | + os.makedirs(dirname, exist_ok=True) |
| 27 | |
| 28 | with open(path, 'w', encoding='utf-8') as f: |
| 29 | f.write(data) |
| 30 | -- |
| 31 | 2.25.1 |
| 32 | |