Skip to content

Commit d8e0a74

Browse files
tqchenMasterJH5574
authored andcommitted
[METAL] Fix flaky memory issue due to racing (apache#14671)
This PR aims to fix a relatively rare flaky race issuse when we are attempting to purge memory and the buffer is still in use in command buffer
1 parent 1fc41fa commit d8e0a74

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/runtime/metal/metal_device_api.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ int GetWarpSize(id<MTLDevice> dev) {
196196

197197
void MetalWorkspace::FreeDataSpace(Device dev, void* ptr) {
198198
AUTORELEASEPOOL {
199+
// need to make sure buffer is not in use in command buffer
200+
// before set the purgeable state to empty
201+
// otherwise can cause issues sometimes
202+
this->StreamSync(dev, nullptr);
199203
// MTLBuffer PurgeableState should be set to empty before manual
200204
// release in order to prevent memory leak
201205
[(id<MTLBuffer>)ptr setPurgeableState:MTLPurgeableStateEmpty];
@@ -336,6 +340,10 @@ int GetWarpSize(id<MTLDevice> dev) {
336340
if (temp_buffer_[dev.device_id] == nil || temp_buffer_[dev.device_id].length < size) {
337341
id<MTLDevice> mtl_dev = MetalWorkspace::Global()->GetDevice(dev);
338342
if (temp_buffer_[dev.device_id] != nil) {
343+
// need to make sure buffer is not in use in command buffer
344+
// before set the purgeable state to empty
345+
// otherwise can cause issues sometimes
346+
MetalWorkspace::Global()->StreamSync(dev, nullptr);
339347
[temp_buffer_[dev.device_id] setPurgeableState:MTLPurgeableStateEmpty];
340348
[temp_buffer_[dev.device_id] release];
341349
}

0 commit comments

Comments
 (0)