{"action":"created","comment":{"url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/comments/24262440","id":24262440,"diff_hunk":"@@ -36,14 +36,17 @@ sparkR.stop <- function(env) {\n   if (exists(\".sparkRjsc\", envir = env)) {\n     sc <- get(\".sparkRjsc\", envir = env)\n     callJMethod(sc, \"stop\")\n+    rm(\".sparkRjsc\", envir = env)\n   }\n ","path":"pkg/R/sparkR.R","position":24,"original_position":24,"commit_id":"ddf5da8192d5e546e5017593c5db60e8a4a7fe38","original_commit_id":"c23ee3fc1e21a0a380ab29d97c3598d0a7733ab8","user":{"login":"shivaram","id":143893,"avatar_url":"https://avatars.githubusercontent.com/u/143893?v=3","gravatar_id":"","url":"https://api.github.com/users/shivaram","html_url":"https://github.com/shivaram","followers_url":"https://api.github.com/users/shivaram/followers","following_url":"https://api.github.com/users/shivaram/following{/other_user}","gists_url":"https://api.github.com/users/shivaram/gists{/gist_id}","starred_url":"https://api.github.com/users/shivaram/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shivaram/subscriptions","organizations_url":"https://api.github.com/users/shivaram/orgs","repos_url":"https://api.github.com/users/shivaram/repos","events_url":"https://api.github.com/users/shivaram/events{/privacy}","received_events_url":"https://api.github.com/users/shivaram/received_events","type":"User","site_admin":false},"body":"@oscaroboto - So technically we would like both to exist at the same time, but that doesn't always happen to be the case. Let me explain a bug that this code path was fixing (and this will also explain why connExists looks the way it does)\r\n\r\nWhen `sparkR.stop` is called we want to do two things (a) stop the SparkContext and (b) stop the backend handler (Actually I am not that sure about (a), but lets postpone that for now). The problem is that to do either of those we need an active connection to the backend\r\n\r\n- On exit from the R shell when one doesn't save the workspace, we usually have an active connection, so everything works fine i.e. `.sparkRCon` and `.sparkRjsc` both exit and the connection is valid. If you remove `.sparkRCon` from the env, this case will be idempotent i.e. you can call `sparkR.stop` many times without worry.\r\n- On exit from the R shell (or RStudio) when one saves the workspace, R closes all active connections before calling the finalizer. So in this case we have a value in `.sparkRCon` but that connection is not valid. This is the main reason we have `connExists` right now is to catch an exception when we call `isOpen`.  Since we definitely want to stop the backend, we re-open a new connection if one does not exist. Now to make this idempotent what we need to do is to use `tryCatch` around the call to `connectBackend`. That way if the backend has already exited and we can't connect to it, we can just return from this function.\r\n- Finally the `sleep(1)` should be only required in cases where we call `stopBackend` - so your call on that is correct. \r\n\r\nIn summary I think the only thing that needs to change as a part of this PR is adding a `tryCatch` to `connectBackend` in this function and of course the `rm(.sparkRJsc)` above. Let me know if my analysis missed some use case.","created_at":"2015-02-06T18:57:05Z","updated_at":"2015-02-06T18:57:05Z","html_url":"https://github.com/amplab-extras/SparkR-pkg/pull/156#discussion_r24262440","pull_request_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/156","_links":{"self":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/comments/24262440"},"html":{"href":"https://github.com/amplab-extras/SparkR-pkg/pull/156#discussion_r24262440"},"pull_request":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/156"}}},"pull_request":{"url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/156","id":28771783,"html_url":"https://github.com/amplab-extras/SparkR-pkg/pull/156","diff_url":"https://github.com/amplab-extras/SparkR-pkg/pull/156.diff","patch_url":"https://github.com/amplab-extras/SparkR-pkg/pull/156.patch","issue_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/issues/156","number":156,"state":"open","locked":false,"title":"export sparkR.stop ","user":{"login":"oscaroboto","id":5174609,"avatar_url":"https://avatars.githubusercontent.com/u/5174609?v=3","gravatar_id":"","url":"https://api.github.com/users/oscaroboto","html_url":"https://github.com/oscaroboto","followers_url":"https://api.github.com/users/oscaroboto/followers","following_url":"https://api.github.com/users/oscaroboto/following{/other_user}","gists_url":"https://api.github.com/users/oscaroboto/gists{/gist_id}","starred_url":"https://api.github.com/users/oscaroboto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oscaroboto/subscriptions","organizations_url":"https://api.github.com/users/oscaroboto/orgs","repos_url":"https://api.github.com/users/oscaroboto/repos","events_url":"https://api.github.com/users/oscaroboto/events{/privacy}","received_events_url":"https://api.github.com/users/oscaroboto/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-02-05T23:26:20Z","updated_at":"2015-02-06T18:57:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"371f373eb55834a0b46b1a843dbe382ed72ff1ad","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/156/commits","review_comments_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/156/comments","review_comment_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/comments/{number}","comments_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/issues/156/comments","statuses_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/statuses/ddf5da8192d5e546e5017593c5db60e8a4a7fe38","head":{"label":"oscaroboto:master","ref":"master","sha":"ddf5da8192d5e546e5017593c5db60e8a4a7fe38","user":{"login":"oscaroboto","id":5174609,"avatar_url":"https://avatars.githubusercontent.com/u/5174609?v=3","gravatar_id":"","url":"https://api.github.com/users/oscaroboto","html_url":"https://github.com/oscaroboto","followers_url":"https://api.github.com/users/oscaroboto/followers","following_url":"https://api.github.com/users/oscaroboto/following{/other_user}","gists_url":"https://api.github.com/users/oscaroboto/gists{/gist_id}","starred_url":"https://api.github.com/users/oscaroboto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oscaroboto/subscriptions","organizations_url":"https://api.github.com/users/oscaroboto/orgs","repos_url":"https://api.github.com/users/oscaroboto/repos","events_url":"https://api.github.com/users/oscaroboto/events{/privacy}","received_events_url":"https://api.github.com/users/oscaroboto/received_events","type":"User","site_admin":false},"repo":{"id":25552581,"name":"SparkR-pkg","full_name":"oscaroboto/SparkR-pkg","owner":{"login":"oscaroboto","id":5174609,"avatar_url":"https://avatars.githubusercontent.com/u/5174609?v=3","gravatar_id":"","url":"https://api.github.com/users/oscaroboto","html_url":"https://github.com/oscaroboto","followers_url":"https://api.github.com/users/oscaroboto/followers","following_url":"https://api.github.com/users/oscaroboto/following{/other_user}","gists_url":"https://api.github.com/users/oscaroboto/gists{/gist_id}","starred_url":"https://api.github.com/users/oscaroboto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oscaroboto/subscriptions","organizations_url":"https://api.github.com/users/oscaroboto/orgs","repos_url":"https://api.github.com/users/oscaroboto/repos","events_url":"https://api.github.com/users/oscaroboto/events{/privacy}","received_events_url":"https://api.github.com/users/oscaroboto/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/oscaroboto/SparkR-pkg","description":"R frontend for Spark","fork":true,"url":"https://api.github.com/repos/oscaroboto/SparkR-pkg","forks_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/forks","keys_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/keys{/key_id}","collaborators_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/teams","hooks_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/hooks","issue_events_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/issues/events{/number}","events_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/events","assignees_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/assignees{/user}","branches_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/branches{/branch}","tags_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/tags","blobs_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/git/refs{/sha}","trees_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/git/trees{/sha}","statuses_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/statuses/{sha}","languages_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/languages","stargazers_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/stargazers","contributors_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/contributors","subscribers_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/subscribers","subscription_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/subscription","commits_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/commits{/sha}","git_commits_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/git/commits{/sha}","comments_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/comments{/number}","issue_comment_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/issues/comments/{number}","contents_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/contents/{+path}","compare_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/compare/{base}...{head}","merges_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/merges","archive_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/downloads","issues_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/issues{/number}","pulls_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/pulls{/number}","milestones_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/milestones{/number}","notifications_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/labels{/name}","releases_url":"https://api.github.com/repos/oscaroboto/SparkR-pkg/releases{/id}","created_at":"2014-10-22T00:08:17Z","updated_at":"2015-02-06T18:30:52Z","pushed_at":"2015-02-06T18:30:51Z","git_url":"git://github.com/oscaroboto/SparkR-pkg.git","ssh_url":"git@github.com:oscaroboto/SparkR-pkg.git","clone_url":"https://github.com/oscaroboto/SparkR-pkg.git","svn_url":"https://github.com/oscaroboto/SparkR-pkg","homepage":"http://amplab-extras.github.io/SparkR-pkg","size":3136,"stargazers_count":0,"watchers_count":0,"language":"R","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"amplab-extras:master","ref":"master","sha":"08ff30b14f86974ac085bd0e9ab95e536839bfc8","user":{"login":"amplab-extras","id":6403540,"avatar_url":"https://avatars.githubusercontent.com/u/6403540?v=3","gravatar_id":"","url":"https://api.github.com/users/amplab-extras","html_url":"https://github.com/amplab-extras","followers_url":"https://api.github.com/users/amplab-extras/followers","following_url":"https://api.github.com/users/amplab-extras/following{/other_user}","gists_url":"https://api.github.com/users/amplab-extras/gists{/gist_id}","starred_url":"https://api.github.com/users/amplab-extras/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amplab-extras/subscriptions","organizations_url":"https://api.github.com/users/amplab-extras/orgs","repos_url":"https://api.github.com/users/amplab-extras/repos","events_url":"https://api.github.com/users/amplab-extras/events{/privacy}","received_events_url":"https://api.github.com/users/amplab-extras/received_events","type":"Organization","site_admin":false},"repo":{"id":15365789,"name":"SparkR-pkg","full_name":"amplab-extras/SparkR-pkg","owner":{"login":"amplab-extras","id":6403540,"avatar_url":"https://avatars.githubusercontent.com/u/6403540?v=3","gravatar_id":"","url":"https://api.github.com/users/amplab-extras","html_url":"https://github.com/amplab-extras","followers_url":"https://api.github.com/users/amplab-extras/followers","following_url":"https://api.github.com/users/amplab-extras/following{/other_user}","gists_url":"https://api.github.com/users/amplab-extras/gists{/gist_id}","starred_url":"https://api.github.com/users/amplab-extras/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amplab-extras/subscriptions","organizations_url":"https://api.github.com/users/amplab-extras/orgs","repos_url":"https://api.github.com/users/amplab-extras/repos","events_url":"https://api.github.com/users/amplab-extras/events{/privacy}","received_events_url":"https://api.github.com/users/amplab-extras/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/amplab-extras/SparkR-pkg","description":"R frontend for Spark","fork":false,"url":"https://api.github.com/repos/amplab-extras/SparkR-pkg","forks_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/forks","keys_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/keys{/key_id}","collaborators_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/teams","hooks_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/hooks","issue_events_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/issues/events{/number}","events_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/events","assignees_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/assignees{/user}","branches_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/branches{/branch}","tags_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/tags","blobs_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/git/refs{/sha}","trees_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/git/trees{/sha}","statuses_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/statuses/{sha}","languages_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/languages","stargazers_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/stargazers","contributors_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/contributors","subscribers_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/subscribers","subscription_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/subscription","commits_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/commits{/sha}","git_commits_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/git/commits{/sha}","comments_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/comments{/number}","issue_comment_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/issues/comments/{number}","contents_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/contents/{+path}","compare_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/compare/{base}...{head}","merges_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/merges","archive_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/downloads","issues_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/issues{/number}","pulls_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls{/number}","milestones_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/milestones{/number}","notifications_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/labels{/name}","releases_url":"https://api.github.com/repos/amplab-extras/SparkR-pkg/releases{/id}","created_at":"2013-12-21T21:16:54Z","updated_at":"2015-02-04T06:48:58Z","pushed_at":"2015-02-04T18:51:34Z","git_url":"git://github.com/amplab-extras/SparkR-pkg.git","ssh_url":"git@github.com:amplab-extras/SparkR-pkg.git","clone_url":"https://github.com/amplab-extras/SparkR-pkg.git","svn_url":"https://github.com/amplab-extras/SparkR-pkg","homepage":"http://amplab-extras.github.io/SparkR-pkg","size":4639,"stargazers_count":382,"watchers_count":382,"language":"R","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":133,"mirror_url":null,"open_issues_count":24,"forks":133,"open_issues":24,"watchers":382,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/156"},"html":{"href":"https://github.com/amplab-extras/SparkR-pkg/pull/156"},"issue":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/issues/156"},"comments":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/issues/156/comments"},"review_comments":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/156/comments"},"review_comment":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/pulls/156/commits"},"statuses":{"href":"https://api.github.com/repos/amplab-extras/SparkR-pkg/statuses/ddf5da8192d5e546e5017593c5db60e8a4a7fe38"}}}}