emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Christophe Rhodes <csr21@cantab.net>
To: emacs-orgmode@gnu.org
Subject: [patch] race condition in org-babel R
Date: Fri, 20 May 2011 12:21:24 +0100	[thread overview]
Message-ID: <87sjs9wrbf.fsf@cantab.net> (raw)

[-- 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

             reply	other threads:[~2011-05-20 11:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-20 11:21 Christophe Rhodes [this message]
2011-05-20 20:37 ` [patch] race condition in org-babel R Dan Davison

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87sjs9wrbf.fsf@cantab.net \
    --to=csr21@cantab.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).