Skip to content

Commit 39dcc1f

Browse files
rdinoffRob Dinoff
andauthored
fix: parsing date fails due to thread race (#477)
Two python threads can call get_date_helper() simultaneously, causing a partially-contstructed DateHelper object to be returned to the caller. Co-authored-by: Rob Dinoff <rob.dinoff@nokia-bell-labs.com>
1 parent 1ca6690 commit 39dcc1f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
### Others
1212
1. [#472](https://github.com/influxdata/influxdb-client-python/pull/472): Drop supports for Python 3.6
1313

14+
### Bug Fixes
15+
1. [#477](https://github.com/influxdata/influxdb-client-python/pull/477): parsing date fails due to thread race
16+
1417
## 1.31.0 [2022-07-29]
1518

1619
### Features

influxdb_client/client/util/date_utils.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Utils to get right Date parsing function."""
22
import datetime
3+
import threading
34
from datetime import timezone as tz
45

56
from dateutil import parser
67

78
date_helper = None
89

10+
lock_ = threading.Lock()
11+
912

1013
class DateHelper:
1114
"""
@@ -79,11 +82,15 @@ def get_date_helper() -> DateHelper:
7982
"""
8083
global date_helper
8184
if date_helper is None:
82-
date_helper = DateHelper()
83-
try:
84-
import ciso8601
85-
date_helper.parse_date = ciso8601.parse_datetime
86-
except ModuleNotFoundError:
87-
date_helper.parse_date = parser.parse
85+
with lock_:
86+
# avoid duplicate initialization
87+
if date_helper is None:
88+
_date_helper = DateHelper()
89+
try:
90+
import ciso8601
91+
_date_helper.parse_date = ciso8601.parse_datetime
92+
except ModuleNotFoundError:
93+
_date_helper.parse_date = parser.parse
94+
date_helper = _date_helper
8895

8996
return date_helper

0 commit comments

Comments
 (0)