Skip to content

Commit

Permalink
fix(NamedEvent): Release semaphore ID in dtor when created with semget (
Browse files Browse the repository at this point in the history
  • Loading branch information
matejk committed Dec 18, 2023
1 parent bf3c519 commit bee5a2a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions Foundation/include/Poco/NamedEvent_UNIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Foundation_API NamedEventImpl
sem_t* _sem;
#else
int _semid; // semaphore id
bool _createdId; // semaphore id was created with this instance
#endif
};

Expand Down
7 changes: 7 additions & 0 deletions Foundation/src/NamedEvent_UNIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ NamedEventImpl::NamedEventImpl(const std::string& name):
if ((long) _sem == (long) SEM_FAILED)
throw SystemException(Poco::format("cannot create named mutex %s (sem_open() failed, errno=%d)", fileName, errno), _name);
#else
_createdId = false;
int fd = open(fileName.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1 && errno == ENOENT)
fd = open(fileName.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Expand All @@ -71,6 +72,7 @@ NamedEventImpl::NamedEventImpl(const std::string& name):
_semid = semget(key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT | IPC_EXCL);
if (_semid >= 0)
{
_createdId = true;
union semun arg;
arg.val = 0;
semctl(_semid, 0, SETVAL, arg);
Expand All @@ -88,6 +90,11 @@ NamedEventImpl::~NamedEventImpl()
{
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
sem_close(_sem);
#else
if (_createdId)
{
semctl(_semid, 0, IPC_RMID);
}
#endif
}

Expand Down
24 changes: 23 additions & 1 deletion Foundation/testsuite/src/NamedEventTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/Timestamp.h"

#include "Poco/Exception.h"

using Poco::NamedEvent;
using Poco::Thread;
Expand Down Expand Up @@ -101,6 +101,27 @@ void NamedEventTest::testNamedEvent()
}


void NamedEventTest::testCreateManyNamedEvents()
{
std::string name;
try
{
int i = 0;
for (; i < 40000; i++)
{
name = std::string("TestEvent ") + std::to_string(i);
NamedEvent* ne = new NamedEvent(name);
delete ne;
}
assertEqual(i, 40000);
}
catch (const Poco::Exception& e)
{
fail (std::string("Failed creating named event: ").append(name).append(" ").append(e.displayText()));
}
}


void NamedEventTest::setUp()
{
}
Expand All @@ -116,6 +137,7 @@ CppUnit::Test* NamedEventTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NamedEventTest");

CppUnit_addTest(pSuite, NamedEventTest, testNamedEvent);
CppUnit_addTest(pSuite, NamedEventTest, testCreateManyNamedEvents);

return pSuite;
}
1 change: 1 addition & 0 deletions Foundation/testsuite/src/NamedEventTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NamedEventTest: public CppUnit::TestCase
~NamedEventTest();

void testNamedEvent();
void testCreateManyNamedEvents();

void setUp();
void tearDown();
Expand Down

0 comments on commit bee5a2a

Please sign in to comment.