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

Problem: zloop is using reader callback after zloop_reader_end #1634

Closed
piskorzj opened this issue Mar 7, 2017 · 10 comments
Closed

Problem: zloop is using reader callback after zloop_reader_end #1634

piskorzj opened this issue Mar 7, 2017 · 10 comments

Comments

@piskorzj
Copy link
Contributor

piskorzj commented Mar 7, 2017

I've recently hit a problem when I have zloop with timer and reader. And inside timer I'm doing zloop_reader_end. All works well until both conditions are met in one zloop go - timer is expired and data is available on reader. In timer after zloop_reader_end I'm also doing zsock_destroy and other cleanup stuff. Thus callback called for socket contains invalid data - I don't want it to be called after doing zloop_reader_end cause it breaks whole loop logic.

Any suggestions how to avoid this or zloop implementation needs to be changed?

@bluca
Copy link
Member

bluca commented Mar 7, 2017

You get a reference to the socket in the callback, can't you simply check if it's still valid and return if not?

@bluca
Copy link
Member

bluca commented Mar 7, 2017

I guess a way to fix this would be to do the same check at https://github.com/zeromq/czmq/blob/master/src/zloop.c#L715 after the timers callbacks and before the pollset is checked at https://github.com/zeromq/czmq/blob/master/src/zloop.c#L770

@piskorzj
Copy link
Contributor Author

piskorzj commented Mar 7, 2017

@bluca How would you see checking socket in callback when zloop on pollset rebuild is making copy of reader struct (as far as I can tell from code here https://github.com/zeromq/czmq/blob/master/src/zloop.c#L265)?

Something like:
if(self->need_rebuild) continue;
after timers to skip rest of processing and start poll again?

@bluca
Copy link
Member

bluca commented Mar 7, 2017

zmq_poll is edge triggered so skipping and going back into it while there's still data to read would be wrong, since it would not return anymore until new data is available

See: https://funcptr.net/2012/09/10/zeromq---edge-triggered-notification/

@piskorzj
Copy link
Contributor Author

piskorzj commented Mar 7, 2017

Ok, then this https://github.com/zeromq/czmq/blob/master/src/zloop.c#L795 is also invalid, isn't it?

@pijyoi
Copy link
Contributor

pijyoi commented Mar 7, 2017 via email

@bluca
Copy link
Member

bluca commented Mar 7, 2017

@piskorzj you are right indeed, got confused for a moment

@piskorzj
Copy link
Contributor Author

piskorzj commented Mar 7, 2017

Ok :)
I'm writing now test case catching my issue and I'll add that lines with continue to check if it fixes it. Then I'll make pull request.

@bluca
Copy link
Member

bluca commented Mar 7, 2017

perfect, thanks

piskorzj added a commit to piskorzj/czmq that referenced this issue Mar 7, 2017
…#1634

Solution: Rebuild pollset after timers if poll needs to be rebuilt.
piskorzj added a commit to piskorzj/czmq that referenced this issue Mar 7, 2017
…#1634

Solution: Rebuild pollset after timers if poll needs to be rebuilt.
bluca added a commit that referenced this issue Mar 8, 2017
Problem: zloop is using reader callback after zloop_reader_end #1634
@bluca
Copy link
Member

bluca commented Mar 8, 2017

Fixed by #1635

@bluca bluca closed this as completed Mar 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants