| 72 | } |
| 73 | |
| 74 | func (s *SafeStreamCloser) Close() error { |
| 75 | // Set this stream to a closing state. |
| 76 | s.closing.Store(true) |
| 77 | |
| 78 | // Make sure a possible writer does not block the lock forever. We need it, so we can close the writer |
| 79 | // side of the stream safely. |
| 80 | _ = s.stream.SetWriteDeadline(time.Now()) |
| 81 | |
| 82 | // This lock is eventually acquired despite Write also acquiring it, because we set a deadline to writes. |
| 83 | s.lock.Lock() |
| 84 | defer s.lock.Unlock() |
| 85 | |
| 86 | // We have to clean up the receiving stream ourselves since the Close in the bottom does not handle that. |
| 87 | s.stream.CancelRead(0) |
| 88 | return s.stream.Close() |
| 89 | } |
| 90 | |
| 91 | func (s *SafeStreamCloser) CloseWrite() error { |
| 92 | s.lock.Lock() |