Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The semaphore posix wrapper should not be movable. #751

Closed
elfenpiff opened this issue Apr 22, 2021 · 8 comments · Fixed by #1377, #1367 or #1402
Closed

The semaphore posix wrapper should not be movable. #751

elfenpiff opened this issue Apr 22, 2021 · 8 comments · Fixed by #1377, #1367 or #1402
Assignees
Labels
bug Something isn't working globex technical debt unclean code and design flaws
Milestone

Comments

@elfenpiff
Copy link
Contributor

elfenpiff commented Apr 22, 2021

Required information

Observed result or behaviour:
According to the documentations:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_09
https://pubs.opengroup.org/onlinepubs/009695399/functions/sem_open.html

The effect of referring to a copy of the object when locking, unlocking, or destroying it is undefined.

The move operations are copying the underlying sem_t handle which leads to undefined behavior. It is most likely that we did not encounter this issue since the move operation was optimized away with RTVO. But it can become a problem

Expected result or behaviour:
All copy- and move-operations are deleted in posix::Semaphore.

See also #1036

@elfenpiff elfenpiff added bug Something isn't working technical debt unclean code and design flaws labels Apr 22, 2021
@elfenpiff
Copy link
Contributor Author

Actually, it is movable without moving the underlying sem_t management construct when it is not used concurrently while being moved.
The idea would be to set the semaphore value (acquired with sem_getvalue) to the value of the origin in the move operation. The move operation never destroys the semaphore with sem_close/sem_destroy only the dtor does and and then move should be possible.

Since the construction/destruction API of a named semaphore differs from the unnamed semaphore it may makes sense to split them into to abstractions. Semaphore and NamedSemaphore.

@elBoberido
Copy link
Member

This might work for move assignment but you still have the problem with the move ctor since the move-to sem_t would never be initialized.

@elfenpiff
Copy link
Contributor Author

elfenpiff commented Apr 27, 2021

I disagree, in the move ctor it is even easier. Here you just call sem_init or sem_open and both functions offer a parameter where you can set the value of the semaphore.
But when implementing this it is actually a copy operation and not a move operation. So maybe we should remove the move operations and implement only the copy operations.

@elBoberido
Copy link
Member

I think copy is even more dangerous than a move. I would remove both of them like in the STL

@elfenpiff
Copy link
Contributor Author

I agree with you but I really fear the smell of the mowed meadow. Maybe we need to tweak the creation pattern with this a bit. I am thinking of something like:

cxx::optional<Semaphore> mySemaphore;
createSemaphore(mySemaphore).or_else([]{ /*...*/ });

Here we do not require move and the semaphore is initialized in place. What do you think @elBoberido

@elBoberido
Copy link
Member

I think this might be a viable option for some use cases. But then one needs to always call mySemaphore.value().wait() which makes the code harder to read. In the end if the creation of the semaphore fails, what can be done? The programmer usually doesn't do this for fun. Waiting and retrying might be an option, but that should be the rare case and what happens if the retry also doesn't work? Another retry and hoping it will work at some time?

We might need two options. One where the Semaphore is created by its ctor and terminates if it fails, similar to the Mutex and one with the optional where the programmer knows how to recover. One of this places might be RouDi when an application requests a condition variable. But in my opinion it would be better to have this already initialized during the RouDi startup in an object pool. Then termination would again be an option.

Another option would be to have a

cxx::optional<Semaphore> mySemaphore;
auto& mySemaphoreRef = mySemaphore.value();

but this is also cumbersome and requires relative pointer if placed in the shared memory

@elfenpiff
Copy link
Contributor Author

elfenpiff commented May 7, 2021

The developer could also write mySemaphore->wait() instead of mySemaphore.value().wait() but your point is still valid.

At the moment I am playing with the thought to just remove the move operations from the semaphore and we do not work with expected at all.
Through the abstraction we ensure the correct usage of all posix functions so misuse failures cannot happen, the only remaining error sources then are:

  1. mistakes in the abstraction which allow misuse cases
  2. misconfigured system when the system limit for semaphores is reached
  3. corrupt system when someone removes semaphores from the system with rm -rf /dev/sem/*

In all those error cases I don't think that it is useful that the user is informed about it. When the abstraction has a defect the user is screwed and cannot recover and this goes even more for a misconfigured or corrupted system.

So we only work with cxx::Expects and cxx::Ensures and terminate.

@elBoberido or did I miss an error case where we actually require an expected?

@elBoberido
Copy link
Member

the only thing I can currently think of is a failed sem_post with EOVERFLOW but I guess when this happens something else is already completely messed up and a log to the console or cxx::Ensures should be sufficient

elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 19, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 20, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
…semaphore platform does not overflow

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue May 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 16, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 16, 2022
… to ensure type safety, fix gcc warning unused value

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 20, 2022
…ency reasons, fix issue in SignalWatcher test

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 21, 2022
…ignalWatcher made signal safe and added comments to explain signal safe nature

Signed-off-by: Christian Eltzschig <christian.eltzschig@apex.ai>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 21, 2022
Signed-off-by: Christian Eltzschig <christian.eltzschig@apex.ai>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 21, 2022
…nal handler

Signed-off-by: Christian Eltzschig <christian.eltzschig@apex.ai>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…struction

Signed-off-by: Christian Eltzschig <christian.eltzschig@apex.ai>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
… that it can be used from within a signal handler

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…semaphore in the signal watcher and the named pipe

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…and use signal_watcher instead, use UnnamedSemaphore instead of Semaphore in NamedPipe, PeriodicTask, WatchDog, ConditionVariableData

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…the tests for a more efficient busy loop

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
… dds gateway, replace semaphores in test with adaptive wait

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
… notes

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
… to ensure type safety, fix gcc warning unused value

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…ency reasons, fix issue in SignalWatcher test

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…ignalWatcher made signal safe and added comments to explain signal safe nature

Signed-off-by: Christian Eltzschig <christian.eltzschig@apex.ai>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…struction

Signed-off-by: Christian Eltzschig <christian.eltzschig@apex.ai>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…TERM

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…TERM

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…TERM

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…TERM

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
… readability

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
…able in windows

Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit to ApexAI/iceoryx that referenced this issue Jun 23, 2022
Signed-off-by: Christian Eltzschig <me@elchris.org>
elfenpiff added a commit that referenced this issue Jun 24, 2022
…six-wrapper

iox-#751: remove old semaphore posix wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working globex technical debt unclean code and design flaws
Projects
None yet
3 participants