A quick puzzle about security and synchronization

Date:June 6, 2005 / year-entry #140
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050606-34/?p=35433
Comments:    14
Summary:This quick puzzle floated past one of our internal discussion groups. // in process A hEventA = CreateEvent(NULL, FALSE, TRUE, TEXT("MyNamedEvent")); // in process B hEventB = OpenEvent(EVENT_MODIFY_STATE, FALSE, TEXT("MyNamedEvent")); WaitForSingleObject(hEventB, INFINITE); In Process B, the OpenEvent succeeds, but the WaitForSingleObject returns immediately instead of waiting. Explain. [Correction: I confused the matter by passing TRUE as...

This quick puzzle floated past one of our internal discussion groups.

// in process A
hEventA = CreateEvent(NULL, FALSE, TRUE, TEXT("MyNamedEvent"));

// in process B
hEventB = OpenEvent(EVENT_MODIFY_STATE, FALSE, TEXT("MyNamedEvent"));
WaitForSingleObject(hEventB, INFINITE);

In Process B, the OpenEvent succeeds, but the WaitForSingleObject returns immediately instead of waiting. Explain.

[Correction: I confused the matter by passing TRUE as the third parameter, thereby creating an event that is initially signalled. Change it to FALSE so that the event is created non-signalled, in which case the WaitForSingleObject would be expected to wait.]


Comments (14)
  1. AC says:

    Am I missing something or is just that the event is simply created as signaled (the third param in CreateEvent) so Wait… function is not supposed to wait by design?

  2. Skywing says:

    You need to request the SYNCHRONIZE standard access right to an object in order to be able to use WaitForXxx on it. This was not done with the OpenEvent() call — the handle returned by OpenEvent here can only be used with SetEvent/PulseEvent/ResetEvent.

  3. Bob says:

    Because the event is created in a signaled state (3rd parameter of TRUE). The wait in the 2nd process will see that and return.

  4. Karl says:

    CreateEvent’s third parameter is bInitialState: "If this parameter is TRUE, the initial state of the event object is signaled; otherwise, it is nonsignaled." (MSDN)

    Here, it is TRUE, hence the event is in the signaled state.

    Now, "The WaitForSingleObject function returns when the specified object is in the signaled state or the time-out interval elapses." (MSDN)

    So, WaitForSingleObject returns.

    Or did I miss something?

  5. Wound says:

    It seems to me as if both AC’s and skywings comments are correct. Assuming that hEventA is created first, then even if the desired access for open event was (EVENT_MODIFY_STATE | SYNCHRONIZE) the wait would return immediately. The MSDN docs also say that the event ought to have a "Local" or "Global" prefix to cope with fast user switching.

  6. Denis says:

    The event existed prior to the call to CreateEvent (from process C). Process C created this as a Manual Reset Event initially set. The params for the type of event and its state are ignored in Process A since the event already exists.

  7. toomuchwin32 says:

    Well you opened the event with EVENT_MODIFY_STATE but not with SYNCHRONIZE. So you can set the event but the handle that you got back with the OpenEvent call does not have permissions to wait on it.

    This musters as a puuzzle in Microsoft? I am really surprised

  8. Ignacio Burgueño says:

    The desired access param for the OpenEvent call should additionally have SYNCHRONIZE rights in order to use the handle in a WaitForSingleObject, right?

  9. Thales says:

    What does WaitForSingleObject return?

  10. Normski says:

    The 3rd parameter of the CreateEvent function states that the initial condition of the event will be set to this, which is in this case TRUE. WaitForSingleObject see that the event has been singled therefore return immediately.

  11. AC says:

    Re: the initial confusion: Even the first time I saw CreateEvent (it was really long long ago) I found these two BOOL parameters very, very unlucky. I never tried to remember which is which. I immediatelly made two my functions like CreateManualResetEvent and CreateAutoResetEvent. One BOOL less, the remaining one was then clearly the initial state, and the nature of the event was also readable and easy to be found with "Find in files".

    Alternatively, the second parameter should have been made accepting some EV_MANUAL or EV_AUTO constants and not BOOL. Of course it can’t be changed anymore, but I wanted to point at it as the example of the unlucky design decision.

  12. Rich Dutton says:

    If the named event already exists CreateEvent does not fail, as one may expect, but rather returns a handle to the already created object. In the case I would imagine that the event was created by another (possibly malicious) process that set the event on creation.

  13. Watch your access masks.

  14. Norman Diamond says:

    Tuesday, June 07, 2005 7:39 AM by AC

    > I found these two BOOL parameters very, very

    > unlucky.

    Actually one is bad enough, like an MFC function that copies data between UI controls and class variables. Which direction is TRUE and which direction is FALSE?

    > Alternatively, the second parameter should

    > have been made accepting some EV_MANUAL or

    > EV_AUTO constants and not BOOL. Of course it

    > can’t be changed anymore,

    Yes it can. For backwards compatibility the BOOL values will still be accepted, but additional definitions can be added for human-readable enum values or macros. It isn’t even necessary to change Microsoft’s DLLs. Just a change to .h files and MSDN pages can make it possible for programmers to add more human-readability to new code.

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index