A small service assembles a charge request from a shared queue and metadata. Its behavior needs to be fixed so that it does not hold the lock while preparing the response and correctly reports the first read error from the input set.
prepare_charge method reads data from shared State under a Mutex and returns Result<ChargePreview, ChargeError>.request_id, amount, currency, labels.Mutex lock must be held only while extracting data from the state, not while formatting and building ChargePreview.prepare_charge call must not change the count of successful preparations if the result is Err.cargo test
The test suite includes a scenario where preparing one request causes a reentrant access to the state from another thread. It must complete without waiting on its own lock.
In another scenario, the input metadata contains an error in the middle of the list. The error must be returned outward, not hidden behind a partially successful result.
Ok(...) with the current request data.Err(...) with that error.Use line comments to explain the problem, its impact, the fix and how to test it.