Skip to content

Commit d14c0bc

Browse files
committed
Fix behavior of CreateOtelProcessContextMapping when the process is forked
1 parent 35c19d0 commit d14c0bc

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

internal/otelcontextmapping_linux.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
otelContextMappingSize = 2 * os.Getpagesize()
3535

3636
existingMappingBytes []byte
37+
publisherPID int
3738
)
3839

3940
type processContextHeader struct {
@@ -45,12 +46,14 @@ type processContextHeader struct {
4546
}
4647

4748
func CreateOtelProcessContextMapping(data []byte) error {
48-
if existingMappingBytes != nil {
49+
if existingMappingBytes != nil && publisherPID == os.Getpid() {
4950
// Unmap the previous mapping if it exists
5051
err := unix.Munmap(existingMappingBytes)
5152
if err != nil {
5253
return fmt.Errorf("failed to munmap previous mapping: %w", err)
5354
}
55+
existingMappingBytes = nil
56+
publisherPID = 0
5457
}
5558

5659
headerSize := int(unsafe.Sizeof(processContextHeader{}))
@@ -106,5 +109,6 @@ func CreateOtelProcessContextMapping(data []byte) error {
106109
)
107110

108111
existingMappingBytes = mappingBytes
112+
publisherPID = os.Getpid()
109113
return nil
110114
}

0 commit comments

Comments
 (0)