Opened 2 years ago

Closed 2 years ago

#10889 closed bug (invalid)

Compiled software can not read UTF-8 characters using readFile when run via Upstart

Reported by: SiXoS Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.10.2
Keywords: Cc:
Operating System: Linux Architecture: Unknown/Multiple
Type of failure: Runtime crash Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Honestly I dont know if this is a problem with Upstart or ghc.

I'm Using Ubuntu 14

Create a software that reads from a file containing UTF-8 characters. Compile the program. Running the program from the terminal works but not if you run it via Upstart. It will crash with the following error: "hGetContents: invalid argument (invalid byte sequence)"

Minimal Example: Main.hs:

module Main where

main :: IO ()
main = readFile "ex.txt" >>= putStr

ex.txt:

å

Place ex.txt and the compiled program "Comp" in the same directory "/the/path/"

/etc/init/bug-test.conf:

start on runlevel [2345]
stop on runlevel [^2345]

chdir /the/path/

exec ./Comp

If you run ./Comp from the terminal it works. But if you run "sudo service bug-test start" and then check "/var/log/upstart/bug-test.log" you will see that it has crashed.

Change History (1)

comment:1 Changed 2 years ago by nomeata

Resolution: invalid
Status: newclosed

This is expected. I assume that under upstart, programs are run without a locale being set, so your Haskell program does not know what encoding to use when reading that file, defaults to (I believe) ascii and fails to decode your file.

There are basically two solution:

  • Change the upstart script to set a locale (e.g. LANG=C.utf-8).
  • Change the program to explicitly set the encoding you want it to use (see hSetEncoding in System.IO)
Note: See TracTickets for help on using tickets.