Commit b407ea1
committed
gh-157500: Serialize time.tzset() with localtime and strftime
`tzset()` mutates process-global libc timezone state (`tzname`, zoneinfo, `tm_zone` pointers). Concurrent `time.tzset()` and `time.localtime()` / `time.strftime('%Z')` is a TSan data race; `tm_zone` can point at storage the other thread just freed.
Hold a `PyMutex` on `time_module_state` around every `time` module path that calls `tzset()`, `localtime_r()`, or reads `tm_zone` / `tzname`. Copy `tm_zone` onto the stack before dropping the lock so `tmtotuple()` does not decode a pointer the next `tzset()` could free.
This only serializes CPython's `time` APIs with each other. Direct libc `tzset()` / `localtime()` from other C code in the process is unchanged.
Fixes #1575001 parent 4d3b405 commit b407ea1
3 files changed
Lines changed: 87 additions & 4 deletions
File tree
- Lib/test
- Misc/NEWS.d/next/Library
- Modules
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
503 | 503 | | |
504 | 504 | | |
505 | 505 | | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
506 | 544 | | |
507 | 545 | | |
508 | 546 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
| |||
567 | 570 | | |
568 | 571 | | |
569 | 572 | | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
570 | 588 | | |
571 | 589 | | |
572 | 590 | | |
| |||
575 | 593 | | |
576 | 594 | | |
577 | 595 | | |
578 | | - | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
579 | 600 | | |
| 601 | + | |
580 | 602 | | |
581 | 603 | | |
582 | 604 | | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
583 | 608 | | |
584 | 609 | | |
585 | 610 | | |
| |||
588 | 613 | | |
589 | 614 | | |
590 | 615 | | |
| 616 | + | |
591 | 617 | | |
592 | 618 | | |
593 | 619 | | |
| |||
884 | 910 | | |
885 | 911 | | |
886 | 912 | | |
887 | | - | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
888 | 916 | | |
| 917 | + | |
| 918 | + | |
889 | 919 | | |
890 | 920 | | |
891 | 921 | | |
| |||
941 | 971 | | |
942 | 972 | | |
943 | 973 | | |
| 974 | + | |
944 | 975 | | |
945 | 976 | | |
| 977 | + | |
946 | 978 | | |
947 | 979 | | |
948 | 980 | | |
| |||
1043 | 1075 | | |
1044 | 1076 | | |
1045 | 1077 | | |
1046 | | - | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
1047 | 1081 | | |
| 1082 | + | |
| 1083 | + | |
1048 | 1084 | | |
1049 | 1085 | | |
1050 | 1086 | | |
| |||
1069 | 1105 | | |
1070 | 1106 | | |
1071 | 1107 | | |
1072 | | - | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
1073 | 1111 | | |
| 1112 | + | |
| 1113 | + | |
1074 | 1114 | | |
1075 | 1115 | | |
1076 | 1116 | | |
| |||
1174 | 1214 | | |
1175 | 1215 | | |
1176 | 1216 | | |
| 1217 | + | |
1177 | 1218 | | |
1178 | 1219 | | |
1179 | 1220 | | |
1180 | 1221 | | |
1181 | 1222 | | |
1182 | 1223 | | |
| 1224 | + | |
1183 | 1225 | | |
1184 | 1226 | | |
1185 | 1227 | | |
| 1228 | + | |
1186 | 1229 | | |
1187 | 1230 | | |
1188 | 1231 | | |
| |||
0 commit comments