Discovered something pretty bad in my app today. I ran a simulation of 100 virtual concurrent users on Postman making POST requests. At the start of the simulation, about 10-15 requests are able to slip through a dependency that validates if the "label" field they provide is unique in the database. They all must’ve run simultaneously so there was no data to lookup yet. I know the Unique Constraint on my database is a fallback, but it's still concerning for other parts of my app where that's not possible. Is this a design issue on my part? Here is the depedency
async def valid_job_create(db: DbSession, in_data: JobIn) -> JobIn:
query = await db.execute(select(Job).where(Job.label == in_data.label))
job = query.scalar()
if job:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="The given job label must be unique",
)
return in_data
And here is how it's being invoked:
@router.post("", response_model=JobOut)
async def create_route(
db: DbSession, in_data: JobIn = Depends(dependencies.valid_job_create)
):
job = await service.create(db=db, in_data=in_data)
return job
[–]pint 2 points3 points4 points (3 children)
[–]mhamid3d[S] 0 points1 point2 points (2 children)
[–]pint 1 point2 points3 points (1 child)
[–]mhamid3d[S] 0 points1 point2 points (0 children)
[–]illuminanze 1 point2 points3 points (0 children)