safx/ObservableArray-RxSwift

Not thread safe

Opened this issue · 0 comments

I was looking at the implementation and I didn't see any locking mechanism for inserting/removing the elements. So I did quick test:

    func testThreadSafety() {
        var a: ObservableArray<String> = ["foo"]

        _ = expectationWithDescription("")
        a.rx_events().subscribeNext { _ in }
            .addDisposableTo(disposeBag)

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            for _ in 0 ..< Int.max {
                a.append("buzz")
            }
        }

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
            for _ in 0 ..< Int.max where a.isEmpty != true {
                if a.isEmpty == false  {
                    a.removeFirst()
                }
            }
        }

        waitForExpectationsWithTimeout(1) { (error) in
            XCTAssertNil(error, "\(error)")
        }
    }

This will make the lib crash.