Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: File in use by other process, how to unuse ?



Permlink Replies: 6 - Last Post: Jan 5, 2017 9:51 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Asger Joergensen

Posts: 370
Registered: 11/18/08
File in use by other process, how to unuse ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2017 4:02 PM
Hi

I have several computers (programs) using the same file on a server,
one computer(program) i writing exclusively and the others are reading.
What do I do, if one of my programs terminate with an open handle to
the file, then the file will always be in use and not accessible for
the other computers.

How to deal with such a situation, without restarting all computers ??

Thanks in advance
Best regards
Asger
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: File in use by other process, how to unuse ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2017 4:28 PM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger wrote:

What do I do, if one of my programs terminate with an open
handle to the file, then the file will always be in use and not
accessible for the other computers.

Not true. The OS automatically closes any open handles when a process terminates.

--
Remy Lebeau (TeamB)
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: File in use by other process, how to unuse ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2017 4:56 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy

Remy Lebeau (TeamB) wrote:

Asger wrote:

What do I do, if one of my programs terminate with an open
handle to the file, then the file will always be in use and not
accessible for the other computers.

Not true. The OS automatically closes any open handles when a process terminates.

Great!!
Thanks.

Over the years I've had many files that I couldn't delete or rename and
I always thought it was because a program hadn't closed a handle, but you
are right (just tested :-) ), så I guess there must have been another reason.

Thanks again
Best regards
Asger
Jan Dijkstra

Posts: 206
Registered: 11/4/99
Re: File in use by other process, how to unuse ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2017 11:58 PM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:
Hi Remy

Remy Lebeau (TeamB) wrote:

Asger wrote:

What do I do, if one of my programs terminate with an open
handle to the file, then the file will always be in use and not
accessible for the other computers.

Not true. The OS automatically closes any open handles when a process terminates.

Great!!
Thanks.

Over the years I've had many files that I couldn't delete or rename and
I always thought it was because a program hadn't closed a handle, but you
are right (just tested :-) ), så I guess there must have been another reason.

Thanks again
Best regards
Asger

I don't know about other OS-es, but on a Windows server files sometimes retain a state of in-use, despite no process having the file open. It doesn't happen often, but the problem raises it's ugly head from time to time.
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: File in use by other process, how to unuse ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2017 1:54 AM   in response to: Jan Dijkstra in response to: Jan Dijkstra
Hi Jan

Jan Dijkstra wrote:


I don't know about other OS-es, but on a Windows server files sometimes retain
a state of in-use, despite no process having the file open. It doesn't happen
often, but the problem raises it's ugly head from time to time.

Are you using ::CreateFile ?
Because have seen ::CreateFile give ERROR_SHARING_VIOLATION where
there was no process using the file and when using FileCreate or
TFileStream everything worked fine.

https://support.microsoft.com/en-us/kb/316609

And I found this in another NG:

Check out the NET FILE command:

C:\>net help file
The syntax of this command is:

NET FILE
[id [/CLOSE]]

NET FILE closes a shared file and removes file locks. When used without
options, it lists the open files on a server. The listing includes the
identification number assigned to an open file, the pathname of the
file, the username, and the number of locks on the file.

This command works only on computers running the Server service.

id Is the identification number of the file.
/CLOSE Closes an open file and removes file locks. Type this command
from the server where the file is shared.

Best regards
Asger

Jan Dijkstra

Posts: 206
Registered: 11/4/99
Re: File in use by other process, how to unuse ?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2017 3:37 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:
Hi Jan

Jan Dijkstra wrote:


I don't know about other OS-es, but on a Windows server files sometimes retain
a state of in-use, despite no process having the file open. It doesn't happen
often, but the problem raises it's ugly head from time to time.

Are you using ::CreateFile ?
Because have seen ::CreateFile give ERROR_SHARING_VIOLATION where
there was no process using the file and when using FileCreate or
TFileStream everything worked fine.

https://support.microsoft.com/en-us/kb/316609

It's more the other way around. Wanting to do some work on a file requiring exclusive access, thereby closing all instances of our own application. Then trying to open the file in a different application (not made by us, with exclusive access) and getting an access denied. Checking on the server, only to find that the server doesn't have the file listed as open by anyone.

You need a server reboot to get rid of this ghosting access.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: File in use by other process, how to unuse ? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2017 9:51 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger wrote:

Are you using ::CreateFile ?
Because have seen ::CreateFile give ERROR_SHARING_VIOLATION
where there was no process using the file and when using FileCreate
or TFileStream everything worked fine.

TFileSteam internally uses FileCreate() and FileOpen(). Those functions
internally use CreateFile() on Windows.


ERROR_SHARING_VIOLATION can only happen if the file IS ALREADY OPEN when
you try to open it with access rights that are not compatible with existing
open handles to the file. That article says as much, as does the CreateFile()
documentation. AntiVirus and Backup systems are the usual suspects for this
behavior, but any process can potentially open any file for any reason.
You can't assume you have exclusive access to a file unless you have successfully
opened the file for exclusive access. Otherwise, it is fair game to anyone
with sufficient security rights.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02