{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-java/issues/1609","labels_url":"https://api.github.com/repos/realm/realm-java/issues/1609/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/1609/comments","events_url":"https://api.github.com/repos/realm/realm-java/issues/1609/events","html_url":"https://github.com/realm/realm-java/pull/1609","id":111907966,"number":1609,"title":"Fixed race condition when closing Realms","user":{"login":"cmelchior","id":406066,"avatar_url":"https://avatars.githubusercontent.com/u/406066?v=3","gravatar_id":"","url":"https://api.github.com/users/cmelchior","html_url":"https://github.com/cmelchior","followers_url":"https://api.github.com/users/cmelchior/followers","following_url":"https://api.github.com/users/cmelchior/following{/other_user}","gists_url":"https://api.github.com/users/cmelchior/gists{/gist_id}","starred_url":"https://api.github.com/users/cmelchior/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmelchior/subscriptions","organizations_url":"https://api.github.com/users/cmelchior/orgs","repos_url":"https://api.github.com/users/cmelchior/repos","events_url":"https://api.github.com/users/cmelchior/events{/privacy}","received_events_url":"https://api.github.com/users/cmelchior/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/realm/realm-java/labels/Review","name":"Review","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-16T20:47:12Z","updated_at":"2015-10-16T21:19:18Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/realm/realm-java/pulls/1609","html_url":"https://github.com/realm/realm-java/pull/1609","diff_url":"https://github.com/realm/realm-java/pull/1609.diff","patch_url":"https://github.com/realm/realm-java/pull/1609.patch"},"body":"Fixes #1608 \r\n\r\nTo qoute Dianne Hackborn: \"Once you think you understand concurrency, It will jump right back and bite you\" :(\r\nAlso WTF is a method called `removeCallbacksAndMessages` not removing the only thing in the Handler API actually called Callback.\r\n\r\nProblem is that we store our handlers in a ConcurrentHashMap. This means that if we start iterating over them in another thread it is over a copy of the map, so if a Realm is closed in the meantime on the original thread, the iterator will still post messages to it. At the same time, we have no way of telling the handler on the original thread to actually stop handling messages.\r\n\r\nOnly way around this would be to synchronize access to all our handlers, which I want to avoid for performance reasons as the Handlers are our main way of propagating changes. So instead our `Handler.Callback` now checks if the Realm is actually still open.\r\n\r\n@realm/java \r\n@andrewthecoder \r\n"},"comment":{"url":"https://api.github.com/repos/realm/realm-java/issues/comments/148839269","html_url":"https://github.com/realm/realm-java/pull/1609#issuecomment-148839269","issue_url":"https://api.github.com/repos/realm/realm-java/issues/1609","id":148839269,"user":{"login":"emanuelez","id":473050,"avatar_url":"https://avatars.githubusercontent.com/u/473050?v=3","gravatar_id":"","url":"https://api.github.com/users/emanuelez","html_url":"https://github.com/emanuelez","followers_url":"https://api.github.com/users/emanuelez/followers","following_url":"https://api.github.com/users/emanuelez/following{/other_user}","gists_url":"https://api.github.com/users/emanuelez/gists{/gist_id}","starred_url":"https://api.github.com/users/emanuelez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emanuelez/subscriptions","organizations_url":"https://api.github.com/users/emanuelez/orgs","repos_url":"https://api.github.com/users/emanuelez/repos","events_url":"https://api.github.com/users/emanuelez/events{/privacy}","received_events_url":"https://api.github.com/users/emanuelez/received_events","type":"User","site_admin":false},"created_at":"2015-10-16T21:19:18Z","updated_at":"2015-10-16T21:19:18Z","body":"Nice! Is there any way to write a test that exposes the bug this PR fixes?"}}