Skip to content
This repository was archived by the owner on Sep 30, 2026. It is now read-only.

[xpu] Fix XpuptiProfilerTest link error and double free crash - #1349

Closed
chuanqi129 wants to merge 1 commit into
pytorch:mainfrom
chuanqi129:fix_xpu_test_build
Closed

chuanqi129 wants to merge 1 commit into
pytorch:mainfrom
chuanqi129:fix_xpu_test_build

Conversation

@chuanqi129

Copy link
Copy Markdown
Contributor

Summary

Fix two build/runtime issues in the XpuptiProfilerTest target on Linux.

Problem

1. Link error: missing pthread

The XpuptiProfilerTest executable (built via ExternalProject) links against libgtest.a using raw file paths from find_library, which loses gtest's transitive pthread dependency. This causes:

undefined reference to symbol 'pthread_setspecific@@GLIBC_2.2.5'
error adding symbols: DSO missing from command line

2. Runtime crash: double free

libgtest.a (static) was embedded into both libXpuptiProfilerTestLib.so and the XpuptiProfilerTest executable, creating two copies of gtest's global UnitTest singleton. At process exit, both copies' destructors fire, causing:

free(): double free detected in tcache 2

Fix

  • test/xpupti/CMakeLists.txt: Remove gtest from the shared library's link dependencies. The shared library only needs gtest headers for compilation (via target_include_directories), not the library itself. Gtest symbols are resolved at runtime from the executable.

  • test/xpupti/compute/CMakeLists.txt:

    • Add Threads::Threads to fix the missing pthread dependency.
    • Add -Wl,--export-dynamic so the executable exports its gtest symbols into the dynamic symbol table, allowing the shared library to resolve them at runtime.

This ensures only one copy of gtest exists in the process, eliminating the double free.> undefined reference to symbol 'pthread_setspecific@@GLIBC_2.2.5'
error adding symbols: DSO missing from command line


**2. Runtime crash: double free**

`libgtest.a` (static) was embedded into both `libXpuptiProfilerTestLib.so` and the `XpuptiProfilerTest` executable, creating two copies of gtest's global `UnitTest` singleton. At process exit, both copies' destructors fire, causing:

free(): double free detected in tcache 2


## Fix

- **`test/xpupti/CMakeLists.txt`**: Remove gtest from the shared library's link dependencies. The shared library only needs gtest headers for compilation (via `target_include_directories`), not the library itself. Gtest symbols are resolved at runtime from the executable.

- **`test/xpupti/compute/CMakeLists.txt`**:
  - Add `Threads::Threads` to fix the missing pthread dependency.
  - Add `-Wl,--export-dynamic` so the executable exports its gtest symbols into the dynamic symbol table, allowing the shared library to resolve them at runtime.

This ensures only one copy of gtest exists in the process, eliminating the double free.

@meta-cla meta-cla Bot added the cla signed label Apr 6, 2026
@chuanqi129 chuanqi129 mentioned this pull request Apr 6, 2026

@scotts scotts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@chuanqi129

Copy link
Copy Markdown
Contributor Author

Thanks @scotts for the review, seems the failed test is not related this PR change

Comment on lines +31 to +37
set(LIB_LINK_LIBRARIES
kineto_base
kineto_api
$<BUILD_INTERFACE:fmt::fmt-header-only>
${SYCL_LIBRARY}
${PTI_LIBRARY}
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats the list defined earlier in lines 9-16.
This list should be defined in one place only for better maintainability.
As the 2nd one is shorter by one entry the 1st one could be shortened by this entry and this entry added only where is needed.

@scotts

scotts commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

I'm confident that the CUDA test failures are unrelated:

  1. I've seen similar failures on other PRs.
  2. This PR only changes XPU builds.

@meta-codesync

meta-codesync Bot commented Apr 8, 2026

Copy link
Copy Markdown

@scotts has imported this pull request. If you are a Meta employee, you can view this in D100049083.

@meta-codesync

meta-codesync Bot commented Apr 8, 2026

Copy link
Copy Markdown

@scotts merged this pull request in eea2334.

scotts added a commit to scotts/pytorch that referenced this pull request Apr 10, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Apr 10, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
Pull Request resolved: #179906
Approved by: https://github.com/ryanzhang22
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
Pull Request resolved: pytorch#179906
Approved by: https://github.com/ryanzhang22
ani300 pushed a commit to IBM/kineto-spyre that referenced this pull request Jul 28, 2026
Summary:
Fix two build/runtime issues in the `XpuptiProfilerTest` target on Linux.

## Problem

**1. Link error: missing pthread**

The `XpuptiProfilerTest` executable (built via `ExternalProject`) links against `libgtest.a` using raw file paths from `find_library`, which loses gtest's transitive pthread dependency. This causes:
```
undefined reference to symbol 'pthread_setspecific@GLIBC_2.2.5'
error adding symbols: DSO missing from command line
```

**2. Runtime crash: double free**

`libgtest.a` (static) was embedded into both `libXpuptiProfilerTestLib.so` and the `XpuptiProfilerTest` executable, creating two copies of gtest's global `UnitTest` singleton. At process exit, both copies' destructors fire, causing:
```
free(): double free detected in tcache 2
```

## Fix

- **`test/xpupti/CMakeLists.txt`**: Remove gtest from the shared library's link dependencies. The shared library only needs gtest headers for compilation (via `target_include_directories`), not the library itself. Gtest symbols are resolved at runtime from the executable.

- **`test/xpupti/compute/CMakeLists.txt`**:
  - Add `Threads::Threads` to fix the missing pthread dependency.
  - Add `-Wl,--export-dynamic` so the executable exports its gtest symbols into the dynamic symbol table, allowing the shared library to resolve them at runtime.

This ensures only one copy of gtest exists in the process, eliminating the double free.> undefined reference to symbol 'pthread_setspecific@GLIBC_2.2.5'
error adding symbols: DSO missing from command line
```

**2. Runtime crash: double free**

`libgtest.a` (static) was embedded into both `libXpuptiProfilerTestLib.so` and the `XpuptiProfilerTest` executable, creating two copies of gtest's global `UnitTest` singleton. At process exit, both copies' destructors fire, causing:
```
free(): double free detected in tcache 2
```

## Fix

- **`test/xpupti/CMakeLists.txt`**: Remove gtest from the shared library's link dependencies. The shared library only needs gtest headers for compilation (via `target_include_directories`), not the library itself. Gtest symbols are resolved at runtime from the executable.

- **`test/xpupti/compute/CMakeLists.txt`**:
  - Add `Threads::Threads` to fix the missing pthread dependency.
  - Add `-Wl,--export-dynamic` so the executable exports its gtest symbols into the dynamic symbol table, allowing the shared library to resolve them at runtime.

This ensures only one copy of gtest exists in the process, eliminating the double free.

Pull Request resolved: pytorch/kineto#1349

Reviewed By: divyanshk

Differential Revision: D100049083

Pulled By: scotts

fbshipit-source-id: d8915ec0599be3836fec84192db09fd90d57406d
Signed-off-by: Antoni Viros i Martin <[email protected]>
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Includes the following commits:

- Emit USDT logger messages on profiler start/stop (pytorch/kineto#1325) b2103f7
- Fix NCCL metadata missing on GPU kernels due to CUPTI buffer ordering (pytorch/kineto#1354) 360873c
- Modernize target_link_libraries in Cmakelists.txt (pytorch/kineto#1283) 2b26470
- Replace shared_ptr singleton with Meyers singleton in CuptiCallbackApi (pytorch/kineto#1350) f287ee7
- Fix XpuptiProfilerTest link error and double free crash (pytorch/kineto#1349) eea2334
- Fix missing wait_on_cuda_event_record_corr_id in Event Synchronize activities (pytorch/kineto#1318) f689d8a
- approximate clock fast path for arm (pytorch/kineto#1333) ec4a9b0
- Fix ABBA deadlock in bufferCompleted when verbose logging is enabled (pytorch/kineto#1347) 404f8cd
- refactor JSON chrome trace writing (pytorch/kineto#1342) c51fcfe
- Fix GPU trace collection on ROCm by ensuring early rocprofiler-sdk tool registration (pytorch/kineto#1338) 4ff89e8
- Remove tb_plugin CI workflows (pytorch/kineto#1341) e335a7d
- Remove INFO logs from kineto trace metadata to fix invalid JSON (pytorch/kineto#1336) 502c513
- Expose occupany limiting factors (pytorch/kineto#1330) 185fe9c

Authored with Claude.
Pull Request resolved: pytorch#179906
Approved by: https://github.com/ryanzhang22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants