How to deal with the "Cannot change working directory back to the original location" error message?

+1 vote
asked May 21, 2015 by Uwe Keim (860 points)
edited May 21, 2015 by Uwe Keim

I'm sometimes getting the following error message:

Rebex.Net.FtpException

-----------------

Cannot change working directory back to the original location.

-----------------

at Rebex.Net.Ftp.CO(String A)
   at Rebex.Net.Ftp.IO(String A)
   at Rebex.Net.Ftp.PP(String A)
   at ZetaProducerPublishing.Runtime.Protocols.FtpProtocol.FtpEngine.ensureDirectoryCreatedDuringSendFile(String remoteFolderPath, Boolean recursive) in c:\P\Zeta Producer\12\Zeta Producer Publishing\Source\Runtime\Protocols\FtpProtocol\FtpEngine.cs:Zeile 760.
   at ...

I do believe that it is in my source code line:

var exists = _ftp.DirectoryExists(remoteFolderPath);

Although the DirectoryExists does not occur in the above stack trace.

On the other hand, I never call the Rebex.Net.Ftp.PP function.

Environment:

When the error occurred, my code had an FTP connection against an IIS FTP service on a Windows 2012 Server, although I don't know whether this is IIS-specific.

My questions:

  • What could be the cause of this error?
  • How can I deal with this error?

Update 1

This is the log as of request:

----------------------------------------
2015-05-21 21:06:56,440 [7] INFO  - [O] Connecting to zeta-sw.com:21 using Ftp 4.0.5584.0.
2015-05-21 21:06:56,440 [7] INFO  - [O] Using proxy none.
2015-05-21 21:06:56,475 [7] INFO  - [R] 220 Microsoft FTP Service
2015-05-21 21:06:56,479 [7] INFO  - [S] USER SomeUser
2015-05-21 21:06:56,489 [7] INFO  - [R] 331 Password required
2015-05-21 21:06:56,490 [7] INFO  - [S] PASS ***************
2015-05-21 21:06:56,502 [7] INFO  - [R] 230 User logged in.
2015-05-21 21:06:56,504 [7] INFO  - [S] FEAT
2015-05-21 21:06:56,514 [7] INFO  - [R] 211-Extended features supported:
2015-05-21 21:06:56,514 [7] INFO  - [R]  LANG EN*
2015-05-21 21:06:56,514 [7] INFO  - [R]  UTF8
2015-05-21 21:06:56,514 [7] INFO  - [R]  AUTH TLS;TLS-C;SSL;TLS-P;
2015-05-21 21:06:56,514 [7] INFO  - [R]  PBSZ
2015-05-21 21:06:56,514 [7] INFO  - [R]  PROT C;P;
2015-05-21 21:06:56,514 [7] INFO  - [R]  CCC
2015-05-21 21:06:56,514 [7] INFO  - [R]  HOST
2015-05-21 21:06:56,515 [7] INFO  - [R]  SIZE
2015-05-21 21:06:56,515 [7] INFO  - [R]  MDTM
2015-05-21 21:06:56,515 [7] INFO  - [R]  REST STREAM
2015-05-21 21:06:56,515 [7] INFO  - [R] 211 END
2015-05-21 21:06:56,517 [7] INFO  - [S] OPTS UTF8 ON
2015-05-21 21:06:56,527 [7] INFO  - [R] 200 OPTS UTF8 command successful - UTF8 encoding now ON.
2015-05-21 21:06:56,536 [7] INFO  - [S] TYPE I
2015-05-21 21:06:56,546 [7] INFO  - [R] 200 Type set to I.
2015-05-21 21:06:56,547 [7] INFO  - [S] MDTM .
2015-05-21 21:06:56,557 [7] INFO  - [R] 550
2015-05-21 21:06:56,557 [7] INFO  - [S] SIZE .
2015-05-21 21:06:56,567 [7] INFO  - [R] 550
2015-05-21 21:06:56,568 [7] INFO  - [S] PWD
2015-05-21 21:06:56,578 [7] INFO  - [R] 257 "/" is current directory.
2015-05-21 21:06:56,578 [7] INFO  - [S] CWD /projekte.zeta-sw.net
2015-05-21 21:06:56,589 [7] INFO  - [R] 250 CWD command successful.
2015-05-21 21:06:56,589 [7] INFO  - [S] CWD /
2015-05-21 21:06:56,599 [7] INFO  - [R] 250 CWD command successful.
2015-05-21 21:06:56,599 [7] INFO  - [S] PWD
2015-05-21 21:06:56,609 [7] INFO  - [R] 257 "/" is current directory.
2015-05-21 21:06:56,609 [7] INFO  - [S] CWD /projekte.zeta-sw.net/projektname-5
2015-05-21 21:06:56,620 [7] INFO  - [R] 550
2015-05-21 21:06:56,621 [7] INFO  - [S] MKD /projekte.zeta-sw.net/projektname-5
2015-05-21 21:06:56,632 [7] INFO  - [R] 257 "/projekte.zeta-sw.net/projektname-5" directory created.
2015-05-21 21:06:56,644 [7] INFO  - [S] PASV
2015-05-21 21:06:56,654 [7] INFO  - [R] 227 Entering Passive Mode (213,174,38,64,240,174).
2015-05-21 21:06:56,670 [7] INFO  - [S] STOR /projekte.zeta-sw.net/projektname-5/305e22f7-d497-4629-ac92-74bf4af64524.zp12-publish-test
2015-05-21 21:06:56,680 [7] INFO  - [R] 125 Data connection already open; Transfer starting.
2015-05-21 21:06:56,695 [7] INFO  - [R] 226 Transfer complete.
2015-05-21 21:06:56,699 [7] INFO  - [S] SIZE /projekte.zeta-sw.net/projektname-5/305e22f7-d497-4629-ac92-74bf4af64524.zp12-publish-test
2015-05-21 21:06:56,709 [7] INFO  - [R] 213 61
2015-05-21 21:06:56,711 [7] INFO  - [S] DELE /projekte.zeta-sw.net/projektname-5/305e22f7-d497-4629-ac92-74bf4af64524.zp12-publish-test
2015-05-21 21:06:56,721 [7] INFO  - [R] 250 DELE command successful.
2015-05-21 21:06:56,723 [7] INFO  - [S] CWD /projekte.zeta-sw.net/projektname-5
2015-05-21 21:06:56,734 [7] INFO  - [R] 250 CWD command successful.
2015-05-21 21:06:56,742 [7] INFO  - [S] TYPE A
2015-05-21 21:06:56,752 [7] INFO  - [R] 200 Type set to A.
2015-05-21 21:06:56,752 [7] INFO  - [S] PASV
2015-05-21 21:06:56,762 [7] INFO  - [R] 227 Entering Passive Mode (213,174,38,64,240,175).
2015-05-21 21:06:56,775 [7] INFO  - [S] LIST
2015-05-21 21:06:56,785 [7] INFO  - [R] 125 Data connection already open; Transfer starting.
2015-05-21 21:06:56,787 [7] INFO  - [R] 226 Transfer complete.
2015-05-21 21:06:56,789 [7] INFO  - [S] RMD /projekte.zeta-sw.net/projektname-5
2015-05-21 21:06:56,799 [7] INFO  - [R] 250 RMD command successful.
2015-05-21 21:06:56,806 [7] INFO  - [S] TYPE I
2015-05-21 21:06:56,816 [7] INFO  - [R] 200 Type set to I.
2015-05-21 21:06:56,817 [7] INFO  - [S] PWD
2015-05-21 21:06:56,827 [7] INFO  - [R] 257 "/projekte.zeta-sw.net/projektname-5" is current directory.
2015-05-21 21:06:56,827 [7] INFO  - [S] CWD /projekte.zeta-sw.net
2015-05-21 21:06:56,889 [7] INFO  - [R] 250 CWD command successful.
2015-05-21 21:06:56,889 [7] INFO  - [S] CWD /projekte.zeta-sw.net/projektname-5
2015-05-21 21:06:56,900 [7] INFO  - [R] 550
2015-05-21 21:06:56,902 [7] INFO  - [O] Rebex.Net.FtpException: Cannot change working directory back to the original location.
   bei Rebex.Net.Ftp.CO(String A)
   bei Rebex.Net.Ftp.IO(String A)
   bei Rebex.Net.Ftp.PP(String A)

----------------------------------------
Applies to: Rebex FTP/SSL

1 Answer

+1 vote
answered May 21, 2015 by Tomas Knopp (58,890 points)
selected May 25, 2015 by Uwe Keim
 
Best answer

Hello,

the error occured within our Ftp.DirectoryExists method. Please create a log of communication and then either post it here or send it to support@rebex.net so that we are able to see what was actually going on and possibly enhance our routine to also support your FTP server.

Actually, the FTP protocol does not have direct support for checking directory existence, so we have to simulate it via other FTP commands like MLST and MDTM. It looks like your server does not seem to support the advanced commands so as a last remedy we simply try to change into the directory and then change back to see whether it is a directory or not. The error that you got occured when changing back to the original working directory.

commented May 21, 2015 by Uwe Keim (860 points)
Thanks, I've updated my question with the requested log file.
commented May 25, 2015 by Tomas Knopp (58,890 points)
Thank you for the log file.

Now I can see what caused the error from the log.  When the DirectoryExists method was in progress , and just before it attempted to change back to the original working directory (CWD /projekte.zeta-sw.net/projektname-5), then the "/projekte.zeta-sw.net/projektname-5 directory"  was actually deleted by the Rebex Ftp!

We do not send the RMD command within our DirectoryExists method, so could you please check on that? Maybe another instance of Rebex FTP might have deleted the directory which resulted in the error...
commented May 25, 2015 by Uwe Keim (860 points)
The deletion of the directory was done by my code.

How can I ensure that the `DirectoryExists` method works correctly, even if I delete directories on purpose?
commented May 25, 2015 by Tomas Knopp (58,890 points)
You can never assure that the DirectoryExists will work correctly, unless you install/use an FTP server, that supports the MLST command!

Then Rebex FTP client would not have to apply the last remedy workaround that it has to apply with your current FTP server. (Because the normal means to detect whether directory exists failed with your server, we tried changing into the directory to test whether the directory exists. At the same time you deleted the previous working directory which resulted in the error. I do not think deleting the current directory of an active FTP client instance is a good idea and I believe reporting error is the correct thing to do in this case.)
commented May 25, 2015 by Uwe Keim (860 points)
Would it help to disconnect and reconnect when an error occurs and trying the DirectoryExists command again?
commented May 25, 2015 by Lukas Pokorny (101,070 points)
When a "protocol error" like this  occurs (this can be determined by FtpException's Status property), disconnecting and reconnecting is not necessary. Simply changing the current directory to a path that does exist would solve the problem. For example, calling Ftp.ChangeDirectory("/") might be a good idea because it's not possible to delete the root directory (well, at least from inside the FTP session).
...