<< Back to previous view

[CLJ-913] slurp fails on /proc files (linux) Created: 19/Jan/12  Updated: 01/Mar/13  Resolved: 29/Feb/12

Status: Closed
Project: Clojure
Component/s: None
Affects Version/s: Release 1.3
Fix Version/s: None

Type: Defect Priority: Minor
Reporter: Romain Assignee: Unassigned
Resolution: Declined Votes: 0
Labels: None



On my machine,
(slurp "/proc/cpuinfo")
fails with:
java.io.IOException: Argument invalide (NO_SOURCE_FILE:0)

whereas the following succeeds:
(with-open [f (java.io.FileReader. "/proc/cpuinfo")] (slurp f))

Comment by Tassilo Horn [ 23/Jan/12 8:57 AM ]

It seems this is not an error in Clojure but an error in the native implementation of FileInputStream.available(). For example, this Java code throws the same exception.

    FileInputStream fis = new FileInputStream("/home/horn/dead.letter");
    System.out.println(fis.available());  // Works fine

    fis = new FileInputStream("/proc/cpuinfo");
    System.out.println(fis.available());  // IOException is thrown

Tested with sun-jdk and IcedTea 7.2.0.

Comment by Andy Fingerhut [ 20/Feb/12 7:42 PM ]

This is not a fix, but a workaround for this issue with the JVM: call slurp on a reader that you force to be constructed as unbuffered. For example:

(slurp (java.io.InputStreamReader. (java.io.FileInputStream. "/proc/cpuinfo")))

(tested with Sun/Oracle JDK 1.7.0_02)

This is less efficient for large files, so I wouldn't recommend that the slurp code be modified to work this way for all files.

One can imagine an implementation of Clojure slurp and clojure.java.io/reader that first tried the slurp call with buffered I/O, and if an exception like this one occurs fall back to unbuffered I/O. However, that sounds like it would get very hairy very quickly, and would be difficult to write it in such a way that it did not mask I/O errors that should be visible to the caller. Also that seems like unwanted complexity in Clojure to work around what is likely a bug in the JVM. Someone (Tassilo?) filed a bug with Oracle about this exception:


Comment by Andy Fingerhut [ 29/Feb/12 1:06 AM ]

I contacted Romain, the submitter, and they agreed that trying to work around this JVM bug in Clojure's slurp might be a bit excessive, and that closing the ticket was reasonable.

I have added a note about Romain's workaround at http://clojuredocs.org/clojure_core/1.3.0/clojure.core/slurp in hopes that others will be able to find it more easily.

Generated at Thu Apr 24 15:14:26 CDT 2014 using JIRA 4.4#649-r158309.