views:

63

answers:

2

Hi, i know little about pipes but have used one to connect two processes in my code in visual C++. The pipe is working well, but I need to add error handling to the same, hence wanted to know what will happen to a pipe if the server creating it crashed and how do I recognize it from client process?

Also what will happen if the client process tried accessing the same pipe, after the server crash, if no error handling is put in place?

Edit: What impact will be there on the memory if i keep creating new pipes (say by using system time as pipe name) while the previous was broken because of a server crash? Will these broken pipes be removed from the memory?

+1  A: 

IIRC the ReadFile or WriteFile function will return FALSE and GetLastError() will return STATUS_PIPE_DISCONNECTED

I guess this kind of handling is implemented in your code, if not you should better add it ;-)

Vinzenz
And will happen to the broken pipe? Will it be removed from the memory, in case I reuse the same named pipe?
Sneha
You close your handle to the pipe (client handle) and then try to re-connect. It then will either tell you Windows Error Code 2 (File not found) or if the server is started it will be able to reconnect.
Vinzenz
What impact will be there on the memory if i keep on creating new pipes, while the previous one was broken because of server crash?
Sneha
I am not sure what exactly you mean by 'memory impact'. If you do not allocate memory and forget to free it there should be no impact. What kind of impact do you expect?
Vinzenz
I mean, if I keep creating these pipes and these pipes linger in the memory as I am not closing them they are broke by server crash, it would lead to a lot of memory leaks, hampering the performance of other functions which are independent of this part?
Sneha
All your handles from the crashed applications are usually closed by the Operating system, the same is for unfreed memory by the crashing application. All memory and all handles which are not released by the application are released by the Operating System once the application quit. There are few exceptions but those handles are not among them.
Vinzenz
My application has two processes, even if my server process crashes, the other process is up and can continue for a few operations without the process I am considering as server for the pipe, so I was worried if the application does not crash, due to these open handles, can my other process face problem as the memory leak created by the broke pipe would be big.
Sneha
Well you always can implement some forced test crashes in several locations and see if the behavior is as expected. Just that you can be on the secure side. It never hurts to verify such things yourself the amount you learn from this is immense believe me :-)
Vinzenz
I would love to do that, but the deadline to give this application is very close and hence was hoping to find a few quick answers! Thanks anyways.
Sneha
@Sneha: it should take less than 30 minutes to start up the two applications watch some data go across and kill the server app with task manager to see what happens.
Chris Lively
+1  A: 

I just want to throw this out there.

If you want a survivable method for transferring data between two applications, you might consider using Mail Slots or even bringing in BizTalk or another message platform.

There are several things to consider:

  1. what happens if the server is rebooted or loses power?
  2. What happens if the server application becomes unresponsive?
  3. What happens if the server application is killed or goes away completely?
  4. What is the appropriate response of a client application in each of the above?

Each of those contexts represent a potential loss of data. If the data loss is unacceptable then named pipes is not the mechanism you should be using. Instead you need to persist the messages somehow.

Mail Slots, storing to a database, or even leveraging Biztalk can take care of the survivability of the message itself.

If 1 or 3 happens, then the named pipe goes away and must be recreated by a new instance of your server application. If #2 happens, then the pipe won't go away until someone either reboots the server or kills the server app and starts it again.

Regardless, the client application needs to handle the above issues. They boil down to connection failed problems. Depending on what the client does you might have it move into a wait state and let it ping the server every so often to see if it has come back again.

Without knowing the nature of the data and communication processes involved its hard to recommend a proper approach.

Chris Lively