marshmallow-code / marshmallow-code/flask-marshmallow

Race Conditions in Marshmallow - Context Not thread safe?

Open
#233 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
887
Forks
65
Avg merge
7h 25m
Merged PRs (30d)
3

Description

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:

```python
# 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MySchema.custom_field method and the many_schema.dump(objects) call shown in the report. Reproduce serialization with concurrent requests and changing context values, then inspect the Marshmallow and Flask integration paths involved. Done means identifying whether context is shared unsafely and recording a reproducible cause and scope for a fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python, sqlalchemy
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.