elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.37k stars 3.36k forks source link

Receiving unexpected exception from File.cp #12145

Closed jeramyRR closed 2 years ago

jeramyRR commented 2 years ago

Elixir and Erlang/OTP versions

Erlang/OTP 25 [erts-13.0.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

Elixir 1.14.0 (compiled with Erlang/OTP 25)

Operating system

Ubuntu 20.04

Current behavior

I'm wrapping File.cp in case, but I'm never getting to the {:error, reason} clause even though there is an error.

Here's the code:

76: defp copy_file(from_path, to_path) do
77:   case File.exists?(from_path) do
78:      true ->
79:        case File.cp(from_path, to_path) do
80:          :ok ->
81:            Logger.info("Copied #{from_path} to #{to_path}")
82:
83:          {:error, reason} ->
84:            Logger.error("Unable to copy #{from_path} to #{to_path} because: #{inspect(reason)}")
85:
86:     false ->
87:        Logger.error("Unable to copy #{from_path} because it does not exist at this time.")
88:    end
89: end

Here's the error I'm getting:

17:39:13.677 [info] Received new file at: /tmp/.file67.txt.  Copying to temp_loc.
17:39:13.678 [error] GenServer {FileRegistry, ".file67.txt"} terminating
** (File.Error) could not read file stats "/tmp/.file67.txt": no such file or directory
    (elixir 1.14.0) lib/file.ex:408: File.stat!/2
    (elixir 1.14.0) lib/file.ex:1006: File.copy_file_mode!/2
    (elixir 1.14.0) lib/file.ex:1013: File.do_cp_file/4
    (elixir 1.14.0) lib/file.ex:815: File.cp/3
    (ram_disk_daemon 0.1.0) lib/file_mover/file_mover.ex:79: FileMover.FileMover.copy_file/2
    (ram_disk_daemon 0.1.0) lib/file_mover/file_mover.ex:49: FileMover.FileMover.handle_cast/2
    (stdlib 4.0.1) gen_server.erl:1120: :gen_server.try_dispatch/4
    (stdlib 4.0.1) gen_server.erl:1197: :gen_server.handle_msg/6
Last message: {:"$gen_cast", {:new_file, "/tmp/.file67.txt"}}
State: "/tmp/.file67.txt"

Notice that it's on line 79, which is calling File.cp and not File.cp!, which should return {:error, reason}, but instead it looks like an exception is being raised.

Expected behavior

Calling File.cp, not File.cp!, raises an exception when the file is moved in the middle of copying. The expected behavior is for File.cp to return an error tuple {:error, reason} instead of raising an exception.

josevalim commented 2 years ago

Closing in favor of PR, thanks!