Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello CP experts

What is a good way to avoid that a _non cooperative subscriber does block my app?

e.g. this is the code:
http://msdn.microsoft.com/en-us/library/ee850490(v=vs.110).aspx[^]


C#
// What if one of the observers blocks? 
foreach (var observer in observers)
   observer.OnNext(info);

Notifying each subscriber in a separate thread (which I can kill more or less clean) seems for me to be overkill.

Thank you in advance.
Posted
Updated 16-Nov-14 5:46am
v2
Comments
Kornfeld Eliyahu Peter 16-Nov-14 8:20am    
It's a bit thin - can you elaborate on the issue...
[no name] 16-Nov-14 8:37am    
It is simply Observer Pattern....and there is no guaranty that any of the subscribers is cooperative. Means you have to take into account that (because of whatever) it does not return from handler.

E.g.
foreach (var observer in observers)
observer.OnNext(info);

Who who guarantees that OnNext does not block...
Kornfeld Eliyahu Peter 16-Nov-14 8:46am    
You?
An Observer that blocks on OnNext (or other event) is just wrong...
[no name] 16-Nov-14 8:51am    
*lol* I'm working in Praxis not with School examples. No it is not me, it is the user of my class.
Kornfeld Eliyahu Peter 16-Nov-14 9:04am    
As I know you have no way - as today, but part of future features - to set a kind of time-out on an observer's method...
As today your best shot would be Parallel.ForEach...

I think the observer should be agnostic to what a subscriber does. Hence, it should also be agnostic to the threading model of the subscriber. It's the subscriber should be responsible for using some other threads if it is necessary, simply because of the event or delegate mechanism used by an observer already invokes an event or a delegate instance in the same thread if does for all subscribers, synchronous or not.

Note that there is no such thing a miracle. A subscriber either use the subscription in a reasonable way or not. If handing takes too much time, this is what it is. If it is not reasonable, a subscriber should be fixed. There is nothing wrong with it. After all, if you supply a class library to some user of it, you cannot guarantee that it will be used on a correct way. All you can do is to make it clear, behaving in an expected way.

—SA
 
Share this answer
 
Comments
[no name] 16-Nov-14 13:46pm    
Thank you very much for this. I think I have to adapt my mind.
Kind regards, Bruno
Sergey Alexandrovich Kryukov 16-Nov-14 17:50pm    
You are very welcome, Bruno.
—SA
Andreas Gieriet 16-Nov-14 18:48pm    
My 5!
See also my elaboration on detecting the situation in solution #2.
Cheers
Andi
Sergey Alexandrovich Kryukov 16-Nov-14 21:08pm    
Thank you, Andi.
—SA
Depending on your system's requirement, you may employ some kind of watchdog.
E.g. after each subscribers invocation, you may reset some dedicated watchdog timer. If the timer is not reset in due time, the watchdog triggers and does whatever is appropriate for the given requirement (e.g. debug-log the over-time, kill the thread, restart the application, etc.).
Cheers
Andi
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Nov-14 21:20pm    
Frankly, I cannot understand why doing it and how is that related to the question. It's not clear what action is expected after the invocation and why. Note that you need to act on the timer expiration on a separate thread, and the timer action should abort some thread, because this is the only sure way to abort who-knows-what. In advance, it is not know what thread is that. If this is the same thread as the observer's thread, you will have to abort and then restore it (handle ThreadAbortException and then call Thread.ResetAbort). Did you ever try all that? Believe me, this is not a simple part of programming. Worst thing is: you don't know what thread is that, and in case of the subscriber's thread, you don't know if it should be abort-reset or not: it depends on the subscriber's code.
—SA
Andreas Gieriet 16-Nov-14 21:25pm    
Debug-logging and aborting/restarting an application - yes. killing a thread - no. As you mention, this is not easily possible since you don't know what the subscriber does. (I scratched through that option ;-)).
Cheers
Andi
[no name] 17-Nov-14 4:47am    
Thank you Andi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900