prebid-server icon indicating copy to clipboard operation
prebid-server copied to clipboard

Concurrency Issue with ModuleContext in BidderRequest Hook

Open ulphidius opened this issue 10 months ago • 1 comments

During some tests with a custom module, i encountered an issue with the use of ModuleContext. In the BidderRequest hook, when I attempt to replace data from the ModuleContext, i encounter a concurrency issue:

fatal error: concurrent map read and map write

This issue makes sense because I retrieve data stored under a key in the map and then try to update it after performing some operations. Since BidderRequest is triggered for every bidder and the ModuleContext is shared across all hooks of the module, this concurrency problem arises. I guess that will happen to the RawBidderResponse as well.

Theoretically, it is possible to replace ModuleContext with a sync.Map, but this would introduce some performance issues.

Is this behavior intentional ?

ulphidius avatar Feb 26 '25 19:02 ulphidius

I faced the same issue because the stage 'bidder request' shares the same module context across all bidder goroutines. If a hook ends up with a timeout or error, its modification results in the module context should be discarded. I think the easiest way to address this is to add a sync.RWMutex to the module context and expose its lock/rlock/runlock/unlock methods to the modules.

linux019 avatar Mar 08 '25 12:03 linux019