Jroland / kafka-net

Native C# client for Kafka queue servers.
Apache License 2.0
483 stars 232 forks source link

ConcurrentCircularBuffer out of bounds exception #70

Closed akakjs closed 8 years ago

akakjs commented 9 years ago

There are race conditions in the Enqueue method of the ConcurrentCircularBuffer.

The buffer limit is (implemented as a check and "wrap") is two separate Interlocked operations (rather than a single interlocked operation). This could (and does) result in the value of the _head field exceeding _maxSize - 1

        if (Interlocked.Increment(ref _head) > (_maxSize - 1))
            Interlocked.Exchange(ref _head, 0);

This can result in a out of bounds exception when assigning the parameter object to the values array.

        _values[_head] = obj;

I've attached a pull request with a re-implementation of the ConcurrentCircularBuffer without these issues (https://github.com/Jroland/kafka-net/pull/71).