{"action":"created","issue":{"url":"https://api.github.com/repos/nats-io/nats/issues/86","labels_url":"https://api.github.com/repos/nats-io/nats/issues/86/labels{/name}","comments_url":"https://api.github.com/repos/nats-io/nats/issues/86/comments","events_url":"https://api.github.com/repos/nats-io/nats/issues/86/events","html_url":"https://github.com/nats-io/nats/issues/86","id":111269255,"number":86,"title":"Better capture pending writes when getting disconnected","user":{"login":"kozlovic","id":13736691,"avatar_url":"https://avatars.githubusercontent.com/u/13736691?v=3","gravatar_id":"","url":"https://api.github.com/users/kozlovic","html_url":"https://github.com/kozlovic","followers_url":"https://api.github.com/users/kozlovic/followers","following_url":"https://api.github.com/users/kozlovic/following{/other_user}","gists_url":"https://api.github.com/users/kozlovic/gists{/gist_id}","starred_url":"https://api.github.com/users/kozlovic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kozlovic/subscriptions","organizations_url":"https://api.github.com/users/kozlovic/orgs","repos_url":"https://api.github.com/users/kozlovic/repos","events_url":"https://api.github.com/users/kozlovic/events{/privacy}","received_events_url":"https://api.github.com/users/kozlovic/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-10-13T20:56:12Z","updated_at":"2016-01-20T16:00:59Z","closed_at":"2016-01-20T12:20:09Z","body":"Imagine doing nc.Publish() in a loop. The server is stopped/restarted and the library tries to reconnect. The pending buffer is created only in doReconnect(). That leaves a window where nc.bw.write() calls may accumulate to the point of causing a flush (socket write) which will likely result in an error returned from the nc,Publish() call. \r\nAlso, the use of a buffered writer may cause situations where the buffer is flushed and some left over is then appended to the now empty buffer. If a reconnect occurs at the \"right\" time, this data will then be sent to the server, which may cause a parse error (if what was in the buffer was a partial), which would result in another reconnect."},"comment":{"url":"https://api.github.com/repos/nats-io/nats/issues/comments/173249272","html_url":"https://github.com/nats-io/nats/issues/86#issuecomment-173249272","issue_url":"https://api.github.com/repos/nats-io/nats/issues/86","id":173249272,"user":{"login":"kozlovic","id":13736691,"avatar_url":"https://avatars.githubusercontent.com/u/13736691?v=3","gravatar_id":"","url":"https://api.github.com/users/kozlovic","html_url":"https://github.com/kozlovic","followers_url":"https://api.github.com/users/kozlovic/followers","following_url":"https://api.github.com/users/kozlovic/following{/other_user}","gists_url":"https://api.github.com/users/kozlovic/gists{/gist_id}","starred_url":"https://api.github.com/users/kozlovic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kozlovic/subscriptions","organizations_url":"https://api.github.com/users/kozlovic/orgs","repos_url":"https://api.github.com/users/kozlovic/repos","events_url":"https://api.github.com/users/kozlovic/events{/privacy}","received_events_url":"https://api.github.com/users/kozlovic/received_events","type":"User","site_admin":false},"created_at":"2016-01-20T16:00:59Z","updated_at":"2016-01-20T16:00:59Z","body":"Not sure if this captures the original issue I tried to described. Suppose we are connected and here is a view of nc.bw (the '.' indicates free space):\r\n\r\n```\r\nPublish 'abc';\r\n[PUB foo 3\\r\\nabc\\rn....] (4 bytes left)\r\n\r\nPublish 'def'\r\n[PUB foo 3\\r\\nabc\\rnPUB ] => automatically flushed by bufio\r\n[foo 3\\r\\ndef\\rn........] (8 bytes left).\r\n```\r\nAt this point, we disconnect. The content of `nc.bw` is moved to pending. Then say we reconnect, resend the subscriptions (if any) and flush the pending buffer to the newly created connection. We are going to send:\r\n```\r\n[foo 3\\r\\ndef\\rn........]\r\n```\r\nWhich would result in a parse error on the server. Am I missing something?\r\n\r\n"}}