12
12
#include " work/WorkWithCallback.h"
13
13
#include < Tracy.hpp>
14
14
#include < fmt/format.h>
15
+ #include < mutex>
15
16
16
17
namespace stellar
17
18
{
@@ -92,7 +93,9 @@ DownloadBucketsWork::yieldMoreWork()
92
93
std::static_pointer_cast<DownloadBucketsWork>(shared_from_this ()));
93
94
94
95
auto currId = mIndexId ++;
96
+ mIndexMapMutex .lock ();
95
97
auto [indexIter, inserted] = mIndexMap .emplace (currId, nullptr );
98
+ mIndexMapMutex .unlock ();
96
99
releaseAssertOrThrow (inserted);
97
100
98
101
auto successCb = [weak, ft, hash, currId](Application& app) -> bool {
@@ -102,17 +105,20 @@ DownloadBucketsWork::yieldMoreWork()
102
105
// To avoid dangling references, maintain a map of index pointers
103
106
// and do a lookup inside the callback instead of capturing anything
104
107
// by reference.
108
+ self->mIndexMapMutex .lock ();
105
109
auto indexIter = self->mIndexMap .find (currId);
106
110
releaseAssertOrThrow (indexIter != self->mIndexMap .end ());
107
111
releaseAssertOrThrow (indexIter->second );
112
+ auto index = std::move (indexIter->second );
113
+ self->mIndexMap .erase (indexIter);
114
+ self->mIndexMapMutex .unlock ();
108
115
109
116
auto bucketPath = ft.localPath_nogz ();
110
117
auto b = app.getBucketManager ().adoptFileAsBucket <LiveBucket>(
111
118
bucketPath, hexToBin256 (hash),
112
119
/* mergeKey=*/ nullptr ,
113
- /* index=*/ std::move (indexIter-> second ));
120
+ /* index=*/ std::move (index ));
114
121
self->mBuckets [hash] = b;
115
- self->mIndexMap .erase (currId);
116
122
}
117
123
return true ;
118
124
};
0 commit comments