33447

Is there any way to detect whether client pipe's handle is closed when using NamedPipe?

Question:

I am wondering if there is a way to detect the status of client pipe's handle from the server side on Windows platform.

Even though the client closed the pipe(disconnected) with CloseHandle() function, there seems to be no way to detect it from the server side.

Using WaitForSingleObject() with handle object returns WAIT_OBJECT_0, regardless of the status of client handle. So, what would be a good solution to detect whether client pipe's handle is closed or not from server side with less cost?

Answer1:

If you are reading data from the pipe (PIPE_ACCESS_INBOUND or PIPE_ACCESS_DUPLEX) you will get ERROR_BROKEN_PIPE when the client closes their end of the pipe. Even if you aren't ready to process data from the pipe you could start reading it ahead of time (using asynchronous I/O) in order to detect when the pipe is broken.

Note that if there is more than one handle to the client end of the pipe, it will only be considered closed when the last handle to it is closed. This might be an issue, for example, if the client inadvertently causes a subprocess to inherit a copy of the handle.

I don't know of any way to detect that the client has closed an outbound-only pipe without writing data to it. The best option may be to use PIPE_ACCESS_DUPLEX even if the incoming side of the pipe is only ever used to detect when the pipe has been broken.

For future reference, although a handle can be used as a synchronization object, doing so is not recommended and as far as I know the only supported use is to detect the completion of an asynchronous I/O operation where no event object was specified.

Recommend

  • Equivalence of WaitForSingleObject() & ResetEvent() in Linux
  • Directory.CreateDirectory Latency Issue?
  • Timeout and Windows Services for Long Running Processes (Python)
  • Getting Process Information from Process Handle
  • Execute DOS program and get output dynamically
  • security issue with CreateProcess API
  • Can my app be notified when another application starts/stops playing audio?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • How can I sort a a table with VBA with given text condition?
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • print() is showing quotation marks in results
  • How to use an array of arrays with array_map(…) in PHP?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Is there a javascript serializer for JSON.Net?
  • Javascript simulate pressing enter in input box
  • Regex thinks I'm nesting, but I'm not
  • Read text file and split every line in MSBuild
  • Different response to non-authenticated users and AJAX calls
  • Fetching methods from BroadcastReceiver to update UI
  • C# - Serializing and deserializing static member
  • Excel - Autoshape get it's name from cell (value)
  • Incrementing object id automatically JS constructor (static method and variable)
  • Check if a string to interpolate provides expected placeholders
  • Fill an image in a square container while keeping aspect ratio
  • Do create extension work in single-user mode in postgres?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Akka Routing: Reply's send to router ends up as dead letters
  • RestKit - RKRequestDelegate does not exist
  • AT Commands to Send SMS not working in Windows 8.1
  • Traverse Array and Display in markup
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?