{"action":"closed","issue":{"url":"https://api.github.com/repos/ReactiveX/RxJava/issues/2810","labels_url":"https://api.github.com/repos/ReactiveX/RxJava/issues/2810/labels{/name}","comments_url":"https://api.github.com/repos/ReactiveX/RxJava/issues/2810/comments","events_url":"https://api.github.com/repos/ReactiveX/RxJava/issues/2810/events","html_url":"https://github.com/ReactiveX/RxJava/issues/2810","id":60072100,"number":2810,"title":"OperatorPublish bugs","user":{"login":"akarnokd","id":1269832,"avatar_url":"https://avatars.githubusercontent.com/u/1269832?v=3","gravatar_id":"","url":"https://api.github.com/users/akarnokd","html_url":"https://github.com/akarnokd","followers_url":"https://api.github.com/users/akarnokd/followers","following_url":"https://api.github.com/users/akarnokd/following{/other_user}","gists_url":"https://api.github.com/users/akarnokd/gists{/gist_id}","starred_url":"https://api.github.com/users/akarnokd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akarnokd/subscriptions","organizations_url":"https://api.github.com/users/akarnokd/orgs","repos_url":"https://api.github.com/users/akarnokd/repos","events_url":"https://api.github.com/users/akarnokd/events{/privacy}","received_events_url":"https://api.github.com/users/akarnokd/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/ReactiveX/RxJava/labels/Bug","name":"Bug","color":"fc2929"}],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/ReactiveX/RxJava/milestones/13","html_url":"https://github.com/ReactiveX/RxJava/milestones/1.1","labels_url":"https://api.github.com/repos/ReactiveX/RxJava/milestones/13/labels","id":818601,"number":13,"title":"1.1","description":"New functionality requiring a minor version bump.","creator":{"login":"benjchristensen","id":813492,"avatar_url":"https://avatars.githubusercontent.com/u/813492?v=3","gravatar_id":"","url":"https://api.github.com/users/benjchristensen","html_url":"https://github.com/benjchristensen","followers_url":"https://api.github.com/users/benjchristensen/followers","following_url":"https://api.github.com/users/benjchristensen/following{/other_user}","gists_url":"https://api.github.com/users/benjchristensen/gists{/gist_id}","starred_url":"https://api.github.com/users/benjchristensen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benjchristensen/subscriptions","organizations_url":"https://api.github.com/users/benjchristensen/orgs","repos_url":"https://api.github.com/users/benjchristensen/repos","events_url":"https://api.github.com/users/benjchristensen/events{/privacy}","received_events_url":"https://api.github.com/users/benjchristensen/received_events","type":"User","site_admin":false},"open_issues":6,"closed_issues":16,"state":"open","created_at":"2014-10-08T16:24:10Z","updated_at":"2015-05-20T08:27:15Z","due_on":null,"closed_at":null},"comments":1,"created_at":"2015-03-06T08:16:50Z","updated_at":"2015-05-20T08:27:15Z","closed_at":"2015-05-20T08:27:15Z","body":"##### Reconnect issue\r\n\r\nThe following test fails because in OperatorPublish, the ```state.getOrigin()``` still holds the subscriber of a previous connection:\r\n\r\n```java\r\n@Test\r\npublic void testSubscribeAfterDisconnectThenConnect() {\r\n    ConnectableObservable<Integer> source = Observable.just(1).publish();\r\n    \r\n    TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>();\r\n    \r\n    source.subscribe(ts1);\r\n    \r\n    Subscription s = source.connect();\r\n    \r\n    ts1.assertReceivedOnNext(Arrays.asList(1));\r\n    ts1.assertNoErrors();\r\n    ts1.assertTerminalEvent();\r\n    \r\n    TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>();\r\n    \r\n    source.subscribe(ts2);\r\n    \r\n    Subscription s2 = source.connect();\r\n    \r\n    ts2.assertReceivedOnNext(Arrays.asList(1));\r\n    ts2.assertNoErrors();\r\n    ts2.assertTerminalEvent();\r\n    \r\n    System.out.println(s);\r\n    System.out.println(s2);\r\n}\r\n```\r\n\r\nHowever, it works with ```share()```:\r\n```java\r\n@Test\r\npublic void testShareReconnect() {\r\n    Observable<Integer> source = Observable.just(1).share();\r\n    \r\n    TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>();\r\n    \r\n    source.subscribe(ts1);\r\n    \r\n    TestSubscriber<Integer> ts2 = new TestSubscriber<Integer>();\r\n    \r\n    source.subscribe(ts2);\r\n    \r\n    ts1.assertNoErrors();\r\n    ts1.assertTerminalEvent();\r\n    ts1.assertReceivedOnNext(Arrays.asList(1));\r\n    \r\n    ts2.assertNoErrors();\r\n    ts2.assertTerminalEvent();\r\n    ts2.assertReceivedOnNext(Arrays.asList(1));\r\n}\r\n```\r\n\r\n##### connect() is racy\r\n\r\nIn addition, the ```OperatorPublish.connect()``` method is inherently racy: two concurrent connect calls may succeed, one kicking out the upstream subscriber of the other.\r\n\r\n##### OriginSubscriber.requestMore may overflow \r\n\r\n```originOutstanding.addAndGet()``` may turn negative\r\n\r\n##### Retention of subscribers after completion event.\r\n\r\nAfter making all relevant fields and classes package-private, the following test fails:\r\n```java\r\n@Test\r\npublic void testNoSubscriberRetentionOnCompleted() {\r\n    OperatorPublish<Integer> source = (OperatorPublish<Integer>)Observable.just(1).publish();\r\n        \r\n    TestSubscriber<Integer> ts1 = new TestSubscriber<Integer>();\r\n        \r\n    source.unsafeSubscribe(ts1);\r\n        \r\n    source.connect();\r\n\r\n    ts1.assertReceivedOnNext(Arrays.asList(1));\r\n    ts1.assertNoErrors();\r\n    ts1.assertTerminalEvent();\r\n        \r\n    assertTrue(source.requestHandler.state.subscribers.length == 0 \r\n            && source.requestHandler.state.ss.isEmpty());\r\n}\r\n```\r\nNote that blindly evicting values from ss while completing in drainQueues() is wrong too, because if there is a concurrent subscription in the works, its tracking recod in state.ss and subscribers may get deleted as well, instead of letting it wait for the next connect().\r\n\r\n/cc @davidmoten: please look at these issues while you fix OperatorPublish for #2803.\r\n"}}