Skip to content

Wrong munmap() used instead of shmdt() with System V shared memory #302

@jaasco

Description

@jaasco

Problem:
The current code always calls munmap() when freeing shared memory in DataPlaneInSharedMemory::UnmapBuffers, regardless of the actual shared memory backend. However, munmap() is only correct for POSIX shared memory (mmap()), not for System V (shmat()).

When compiled without YANET_USE_POSIX_SHARED_MEMORY, shmget()/shmat() is used, so shmdt() must be called to detach the segment. Otherwise, munmap() fails with EINVAL (22) and memory cleanup is broken.

Example Log Output:
[ERROR] Error munmap 22: Invalid argument

Impact

  • noisy error messages on every restart/shutdown
  • shared-memory segment is not detached → kernel IPC table slowly fills
  • potential memory-leak symptoms in long-running environments

Possible fix

(common/sdpcommon.h)

void UnmapBuffers(uint64_t size) {
    if (!dataplane_data) return;

#ifdef YANET_USE_POSIX_SHARED_MEMORY
    if (munmap(dataplane_data, size) < 0) {
        YANET_LOG_ERROR("Error munmap %d: %s", errno, strerror(errno));
    }
#else
    if (shmdt(dataplane_data) < 0) {
        YANET_LOG_ERROR("Error shmdt %d: %s", errno, strerror(errno));
    }
#endif
    dataplane_data = nullptr;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions