emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [patch] race condition in org-babel R
@ 2011-05-20 11:21 Christophe Rhodes
  2011-05-20 20:37 ` Dan Davison
  0 siblings, 1 reply; 2+ messages in thread
From: Christophe Rhodes @ 2011-05-20 11:21 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 513 bytes --]

Hi,

A substantial fraction of times when exporting my org-babel document
(with many R session code blocks), I get "code block produced no
value".  I think this is because of a race condition between
waiting for the transfer.file to exist and actually populating it with
output; the scenario is: 1. write.table() creates its output file
2. emacs notices its existence, and reads it 3. write.table() gets round
to writing .Last.value to the file.

The attached patch has made exporting much more reliable for me.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-fix-race-condition-in-ob-R-write-object-command.patch --]
[-- Type: text/x-diff, Size: 1539 bytes --]

From 62aa24a7979d687dd5136775004ff7ff5584564a Mon Sep 17 00:00:00 2001
From: Christophe Rhodes <csr21@cantab.net>
Date: Fri, 20 May 2011 12:12:45 +0100
Subject: [PATCH] fix race condition in ob-R write-object-command

* lisp/ob-R.el (org-babel-R-write-object-command): write the object to a
temporary file, then move that temporary file in place to transfer.file
---
 lisp/ob-R.el |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 713d59f..8421c69 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -227,7 +227,7 @@ current code buffer."
 (defvar org-babel-R-eoe-indicator "'org_babel_R_eoe'")
 (defvar org-babel-R-eoe-output "[1] \"org_babel_R_eoe\"")
 
-(defvar org-babel-R-write-object-command "{function(object, transfer.file) {object;invisible(if(inherits(try(write.table(object, file=transfer.file, sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE), silent=TRUE),\"try-error\")) {if(!file.exists(transfer.file)) file.create(transfer.file)})}}(object=%s, transfer.file=\"%s\")")
+(defvar org-babel-R-write-object-command "{function(object, transfer.file) {tfile <- tempfile();invisible(if(inherits(try({write.table(object, file=tfile, sep=\"\\t\", na=\"nil\",row.names=%s, col.names=%s, quote=FALSE); file.rename(tfile,transfer.file)}, silent=TRUE),\"try-error\")){if(!file.exists(transfer.file)) file.create(transfer.file)})}}(object=%s, transfer.file=\"%s\")")
 
 (defun org-babel-R-evaluate
   (session body result-type column-names-p row-names-p)
-- 
1.7.2.5


[-- Attachment #3: Type: text/plain, Size: 19 bytes --]


Best,

Christophe

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [patch] race condition in org-babel R
  2011-05-20 11:21 [patch] race condition in org-babel R Christophe Rhodes
@ 2011-05-20 20:37 ` Dan Davison
  0 siblings, 0 replies; 2+ messages in thread
From: Dan Davison @ 2011-05-20 20:37 UTC (permalink / raw)
  To: emacs-orgmode

Christophe Rhodes <csr21@cantab.net> writes:

> Hi,
>
> A substantial fraction of times when exporting my org-babel document
> (with many R session code blocks), I get "code block produced no
> value".  I think this is because of a race condition between
> waiting for the transfer.file to exist and actually populating it with
> output; the scenario is: 1. write.table() creates its output file
> 2. emacs notices its existence, and reads it 3. write.table() gets round
> to writing .Last.value to the file.
>
> The attached patch has made exporting much more reliable for me.

Thanks, I agree that the race condition seems plausible, and I've
applied a slight modification of your patch (the initial evaluation of
the "object" argument looks pointless, but was there to ensure that
errors in user code are not smothered by the subsequent exception
handling.)

Dan

>
>
>
> Best,
>
> Christophe

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-05-20 20:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-20 11:21 [patch] race condition in org-babel R Christophe Rhodes
2011-05-20 20:37 ` Dan Davison

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).