Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#7676 closed (fixed)

django serializers are not thread safe!

Reported by: sector119 Owned by: nobody
Component: Core (Serialization) Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

HI All!

I have to deserialize, serialize objects in multiple threads with django serializers.

But when I run Deserializer = serializers.get_deserializer(SERIALIZATION_FORMAT) in my threads (more then one, with _one_ thread all works great) I get KeyError? in get_serializer function at return _serializers[format].Deserializer line because _load_serializers does not load all data at _serializers dict before another tread run get_deserializer at this another thread and completely populate serializers dictionary with all, builtin and user-defined serializers! That is why I think that get_(de)?serializers and/or _load_serializers must be thread safe like _populate function at AppCache?!

Attachments (2)

threadsafe_serializers.diff (1.3 KB) - added by mrts 7 years ago.
Threadsafe initialization of the _serializers dict
threadsafe_serializers-same_public_api.diff (3.0 KB) - added by mrts 7 years ago.
Don't break the public API and be explicit about what is and is not thread-safe.

Download all attachments as: .zip

Change History (10)

Changed 7 years ago by mrts

Threadsafe initialization of the _serializers dict

comment:1 Changed 7 years ago by mrts

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

sector119, please test if this fixes the problem.

comment:2 Changed 7 years ago by sector119

Thanks! It works fine for me!

comment:3 Changed 7 years ago by sector119

but why not use threading's RLock, acquire, release ?

comment:4 Changed 7 years ago by mrts

Locking is expensive and should be avoided if possible.

comment:5 Changed 7 years ago by mrts

  • Component changed from Uncategorized to Serialization
  • milestone set to 1.0

As this is a bug, I'd assume it's in scope for 1.0.

Changed 7 years ago by mrts

Don't break the public API and be explicit about what is and is not thread-safe.

comment:6 Changed 7 years ago by serialx

  • Triage Stage changed from Unreviewed to Accepted

Looks Good To Me.

comment:7 Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [8370]) Fixed #7676 -- Modified serializer registration to be thread safe. Thanks to mrts for the patch.

comment:8 Changed 4 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top