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

Correctly call CloseChannel in the destructor of a channel #8248

Closed
wangkuiyi opened this issue Feb 7, 2018 · 0 comments
Closed

Correctly call CloseChannel in the destructor of a channel #8248

wangkuiyi opened this issue Feb 7, 2018 · 0 comments
Assignees

Comments

@wangkuiyi
Copy link
Collaborator

wangkuiyi commented Feb 7, 2018

I just moved @abhinavarora 's PR description into this issue. Thanks to @abhinavarora who discovered this bug.


Problem

The problem here is that if we call the destructor of channel (delete ch) before closing the channel, then the unit test program crashes. We need to make sure that deleting a channel has clean semantics and does not cause program crash.

Cause

On investigating the cause, we found that once the destructor closes the channel, it does not wait for the blocked read and write threads. So in this case, it is likely that we end up destroying the mutex(owned by the channel), when some other thread has locked it. As per the CPP Reference for std::mutex, The behavior of a program is undefined if a mutex is destroyed while still owned by any threads, or a thread terminates while owning a mutex.

Solution

The solution that I and @chengduoZH discussed yesterday was to use read and write reference counters in the channel. When a channel destructor is called, it should wait for readers and writers to return before destroying itself and the mutex. It should block till the counter for readers and writers is not 0.

Thanks to @chengduoZH for investigating and discussing the problem with me.

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

2 participants