{"action":"opened","issue":{"url":"https://api.github.com/repos/hypothesis/h/issues/2590","labels_url":"https://api.github.com/repos/hypothesis/h/issues/2590/labels{/name}","comments_url":"https://api.github.com/repos/hypothesis/h/issues/2590/comments","events_url":"https://api.github.com/repos/hypothesis/h/issues/2590/events","html_url":"https://github.com/hypothesis/h/issues/2590","id":110169183,"number":2590,"title":"Race condition in app initialization","user":{"login":"robertknight","id":2458,"avatar_url":"https://avatars.githubusercontent.com/u/2458?v=3","gravatar_id":"","url":"https://api.github.com/users/robertknight","html_url":"https://github.com/robertknight","followers_url":"https://api.github.com/users/robertknight/followers","following_url":"https://api.github.com/users/robertknight/following{/other_user}","gists_url":"https://api.github.com/users/robertknight/gists{/gist_id}","starred_url":"https://api.github.com/users/robertknight/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/robertknight/subscriptions","organizations_url":"https://api.github.com/users/robertknight/orgs","repos_url":"https://api.github.com/users/robertknight/repos","events_url":"https://api.github.com/users/robertknight/events{/privacy}","received_events_url":"https://api.github.com/users/robertknight/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-10-07T07:48:14Z","updated_at":"2015-10-07T07:48:14Z","closed_at":null,"body":"The current master branch has a race condition in initialization of the app. When the main document's frame connects to the sidebar, `WidgetController.loadAnnotations()` is invoked which tries to get the current focused group in order to construct a search query.\r\n\r\nHowever, if the session state API call to `/app` has not yet completed, the groups service's list of groups is empty and there is no focused group so `groups.focused()` returns null resulting in an exception when constructing the search query.\r\n\r\nThe actual search API call needs to be authenticated so there is no point constructing or dispatching the query until auth completes and the app state has been retrieved. The 'dispatching' part of this is already deferred until auth completes because of the JWT interceptor that is setup on the $http service - deferring all API requests until auth has completed. However the construction of the query in `WidgetController._loadAnnotationsFrom()` should also be deferred until the app state is ready.\r\n\r\nAlthough it is an error in this case to try and use the focused group before the groups list has been loaded, it would be worth considering making groups.focused() non-null by hardcoding the existence of the Public group in the client. In other words, setting the initial state of the session in `session.js` to `{ groups: [<public group>] }` rather than `{}`. However, this would mean that if we add any additional attributes to the public group in future, we would also need to update the object in the client."}}