Skip to content

Race Conditions in Marshmallow - Context Not thread safe? #233

Open
@alnikolaj

Description

@alnikolaj

Hello everybody,

I am having a serious problem. We are serializing a list of objects up to 100 objects at a time/per request and we have some custom field methods.

In these field methods, we pass the user_id in the context and perform some queries specific to that user_id. Now... I have noticed that sometimes this extra object field is wrong and after much investigation I found out that in some cases (within the list of objects in the same request!) the user_id was wrong and suddenly changed. The user_id passed through the context changed mid serialization of these 100 objects... I checked this "wrong" user and saw that his/her last activity was the same as mine what leads me to the conclusion that for some reason Marshmallow is not thread-safe and there is a race condition? Here is an example of my code:

# Schema
class MySchema(SQLAlchemyAutoSchema):
    class Meta:
        model = Object

    extra = fields.Method('custom_field', dump_only=True)

    def custom_field(self, obj):
        user_id = self.context.get('user_id')

        # User Favorites
        favorited = False
        in_favorites = Favorite.query.filter_by(user_id = user_id).first()
        if in_favorites:
            favorited = True
        
        return {
            'user_id' : user_id, 
            'favorited' : favorited
        }

# API
objects = Object.query.all()
user = User.query.get(id=id)

many_schema = MySchema(many=True)
many_schema.context['user_id'] = user.id
return many_schema.dump(objects)

I am unsure how to proceed now. I would need to fix this as soon as possible.

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions