Erlang/OTP Forums

Author Message

<  Erlang questions mailing list  ~  ftp:close and garbage collection

knotwell at f5.com
Posted: Fri May 07, 1999 11:24 pm Reply with quote
Guest
Hello all--

A question. . .I wrote a small ftphammer program. When testing the
program, I noticed that it ran reasonably well at first but after a
short time the performance degraded to unacceptably. Anyhow,
using the process monitor, I noticed that ftp connections appeared to
stay around after leaving the following function:

getData(Host,InFile,OutFile) ->
FP = element(2,ftp:open(Host)),
ftp:user(FP,"anonymous","knotwell_at_f5.com"),
case catch ftp:recv(FP,InFile,OutFile) of
ok -> ok;
Other -> error
end.


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","knotwell_at_f5.com"),
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









Post generated using Mail2Forum (http://m2f.sourceforge.net)
cesarini at csd.uu.se
Posted: Sun May 09, 1999 12:23 pm Reply with quote
Guest
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..
Smile...

{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","knotwell_at_f5.com"),
> 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




Post generated using Mail2Forum (http://m2f.sourceforge.net)

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum