Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abort queued messages if an error occurs #264

Merged
merged 1 commit into from Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion R/execution.r
Expand Up @@ -63,6 +63,7 @@ Executor <- setRefClass(
'Executor',
fields = list(
send_response = 'function',
abort_queued_messages = 'function',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R is weird... You have to pass in the function to make them available in a object? Took a while to find this place...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reference classes are weird. they’re “OO like it works in java, added as an afterhought”

please make the = signs line up. maybe shorten the name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I formatted it, but I value proper names more than short lines...

execution_count = 'integer',
payload = 'list',
err = 'list',
Expand Down Expand Up @@ -258,7 +259,13 @@ execute = function(request) {
}

send_response('execute_reply', request, 'shell', reply_content)


if (interrupted || !is.null(err$ename)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space between ) { (also below 2 times)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

# errors or interrupts should interrupt all currently queued messages,
# not only the currently running one...
abort_queued_messages()
}

if (!silent) {
execution_count <<- execution_count + 1L
}
Expand Down
36 changes: 33 additions & 3 deletions R/kernel.r
Expand Up @@ -91,7 +91,7 @@ new_reply = function(msg_type, parent_msg) {

header <- list(
msg_id = UUIDgenerate(),
username = parent_msg$header$username,
username = parent_msg$header$username,
session = parent_msg$header$session,
msg_type = msg_type,
version = '5.0')
Expand Down Expand Up @@ -129,6 +129,35 @@ handle_shell = function() {
print(c('Got unhandled msg_type:', msg$header$msg_type)))
},

abort_shell_msg = function(){
"Send an abort message for an incoming shell request"
# See https://github.com/ipython/ipykernel/blob/1d97cb2a04149387a0d2dbea1b3d0af691d8df6c/ipykernel/kernelbase.py#L623

parts <- zmq.recv.multipart(sockets$shell, unserialize = FALSE)
msg <- wire_to_msg(parts)
reply_type <- paste(strsplit(msg$header$msg_type, "_")[1], "_reply")
reply_content <- list(status = 'aborted')
send_response(reply_type, msg, "shell", reply_content)
},

abort_queued_messages = function(){
"Abort all already queued shell messages after an error"

while (TRUE) {
ret = zmq.poll(
c(sockets$shell), # only shell channel
c(.pbd_env$ZMQ.PO$POLLIN), # type
0) # zero timeout, only what's already there

if(bitwAnd(zmq.poll.get.revents(1), .pbd_env$ZMQ.PO$POLLIN)) {
abort_shell_msg()
} else {
# no more messages...
break
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... The above break could probably also go to this if as an else clause... Not sure though, I didn't understand the ZMQ stuff good enough and the above seemed safer :-/

}
},

is_complete = function(request) {
"Checks whether the code in the rest is complete"

Expand Down Expand Up @@ -264,8 +293,9 @@ initialize = function(connection_file) {
zmq.bind(sockets$control, url_with_port('control_port'))
zmq.bind(sockets$stdin, url_with_port('stdin_port'))
zmq.bind(sockets$shell, url_with_port('shell_port'))

executor <<- Executor$new(send_response = .self$send_response)

executor <<- Executor$new(send_response = .self$send_response,
abort_queued_messages = .self$abort_queued_messages)
},

run = function() {
Expand Down