ftp:close and garbage collection

F. Cesarini <>
Sun May 9 23:50:54 CEST 1999


When you FTP a file from the shell, you have to type bye to end the
session. The same probably applies with the module provided. 

I haven't seen the code, but a process is usually spawned for
applications with a similar behavior. By executing MODULE:close/1, you
usually terminate the process. MODULE:open or MODULE:start/1 spawns the
process. It is probably more efficient to allow the user to use the same
process to handle several requests than to spawn a process for every new
request. 
Many users (Processes) could concurrently, if given access to the same
FD, use the same process. 

As you are not using any atoms, that is clearly not the problem. 

Two comments on your code, if you don't mind. (I am somewhat brain
washed from my job, and react whenever I see room for improvement..
:)... 

{ok, FP} = ftp:open(Host) 

is much clearer, and will terminate immediately if there was an error in
opening a connection to the host. Had {error, Reason} been returned, 
you would have opened a user session with Reason as a FP, probably
causing a crash in the ftp module. The error would thus be harder to
detect. 

I would call ftp:close/1 even if ftp:recv/3 fails should the process 
still be alive. If a request fails, the resources are probably still
allocated.

Hope this helps,
Francesco

> It turned out I needed to do the following (in order to get everything
> to cleanup okay):
> 
> getData(Host,InFile,OutFile) ->
>     FP = element(2,ftp:open(Host)),
>     ftp:user(FP,"anonymous",""),
>     case catch ftp:recv(FP,InFile,OutFile) of
>         ok    ->
>                 ftp:close(FP),
>                 ok;
>         Other -> error
>     end.
> 
> Assuming that I'm correct in thinking that ftp connections are not
> finalized automatically, is this:
> 
> 1)  related to Erlang's inability to garbage collect atoms
> 2)  enforcing good practice--making the programmer reclaim allocated
>     resources
> 3)  a bug
> 
> A small aside:  it isn't really clear to me why there should be
> separate open and user functions.  I suppose if your first
> username/password didn't work and you wanted to try again without
> forcing the server to spawn another ftpd.
> 
> Thanks.
> 
> --Brad





More information about the erlang-questions mailing list