Skip to content

Commit a5a4830

Browse files
peter-doggartPeter Doggart
andauthored
Added a thread lock to guard first-time schema construction. (#641)
Co-authored-by: Peter Doggart <peter.doggart@pulseai.io>
1 parent 5e1177b commit a5a4830

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Bug Fixes
3535
::
3636

3737
* Add python version requirement on setup.py (#586) [jason-the-j]
38+
* Add a thread lock to avoid concurrent schema construction. (#545) [peter-doggart]
3839
* Fix Nested field schema generation for nullable fields. (#638) [peter-doggart]
3940
* Fix reference resolution for definitions in schema. (#553) [peter-doggart]
4041

flask_restx/api.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import inspect
33
from itertools import chain
44
import logging
5+
import threading
56
import operator
67
import re
78
import sys
@@ -161,6 +162,7 @@ def __init__(
161162
}
162163
)
163164
self._schema = None
165+
self._schema_lock = threading.Lock()
164166
self.models = {}
165167
self._refresolver = None
166168
self.format_checker = format_checker
@@ -567,14 +569,17 @@ def __schema__(self):
567569
:returns dict: the schema as a serializable dict
568570
"""
569571
if not self._schema:
570-
try:
571-
self._schema = Swagger(self).as_dict()
572-
except Exception:
573-
# Log the source exception for debugging purpose
574-
# and return an error message
575-
msg = "Unable to render schema"
576-
log.exception(msg) # This will provide a full traceback
577-
return {"error": msg}
572+
# Guard schema initialization to avoid concurrent construction on first access
573+
with self._schema_lock:
574+
if not self._schema:
575+
try:
576+
self._schema = Swagger(self).as_dict()
577+
except Exception:
578+
# Log the source exception for debugging purpose
579+
# and return an error message
580+
msg = "Unable to render schema"
581+
log.exception(msg) # This will provide a full traceback
582+
return {"error": msg}
578583
return self._schema
579584

580585
@property

0 commit comments

Comments
 (0)