How to Close a File During App Pool Recycle
up vote
0
down vote
favorite
I have an app running in its own app pool that writes often to a text log file. When the 29-hour fixed recycle hits, I usually run into problems because the new process is launched while the old process is still there. This causes "can't open the file because it's in use by another process."
First I simply tried to set Disable Overlapped Cycle to True, but that takes my service off the air for 90 seconds or more, which is not good. If I reduce the Shutdown Time Limit I suppose it would help, but I'd prefer not to kill the process immediately.
I tried setting up an Application_End handler in Global.asax, but I find it's almost never getting called because (I think) my process is still running a thread somewhere (possibly the logging process).
I tried Application_Disposed but so far it does not get called when IIS kills the process after the shutdown time expires, and it's probably too late by then if I go back to overlapped recycle, which I think is preferable.
What I need is a signal from IIS that says "Hey, I want you to stop," that lets me close the log file immediately, but that does not seem to exist.
Any suggestions would be appreciated.
iis application-pool recycling
add a comment |
up vote
0
down vote
favorite
I have an app running in its own app pool that writes often to a text log file. When the 29-hour fixed recycle hits, I usually run into problems because the new process is launched while the old process is still there. This causes "can't open the file because it's in use by another process."
First I simply tried to set Disable Overlapped Cycle to True, but that takes my service off the air for 90 seconds or more, which is not good. If I reduce the Shutdown Time Limit I suppose it would help, but I'd prefer not to kill the process immediately.
I tried setting up an Application_End handler in Global.asax, but I find it's almost never getting called because (I think) my process is still running a thread somewhere (possibly the logging process).
I tried Application_Disposed but so far it does not get called when IIS kills the process after the shutdown time expires, and it's probably too late by then if I go back to overlapped recycle, which I think is preferable.
What I need is a signal from IIS that says "Hey, I want you to stop," that lets me close the log file immediately, but that does not seem to exist.
Any suggestions would be appreciated.
iis application-pool recycling
1
IIS won't shut down your web app (callingApplication_End) until the new web app is running, and that's how overlapped recycle works under the hood. Thus, you should not expect IIS to give you the "signal". Usually it should be your web app that informs of the other copies of itself a new copy is launched by IIS. Or the new copy of your web app implements some simple retry mechanism to write to the file (as once IIS shuts down the old copy of the app, the file is writable).
– Lex Li
Nov 9 at 0:08
I know I won't get Application_End until IIS is really going to end my app--although even that call seems to not happen most of the time. I was hoping there was some other call. Frankly, adding some kind of code to hunt down which W3WP my old app is running in and setting up some IPC channel to communicate with it seems a whole lot of work. At this point I'd consider shutting of recycle completely--it's not helping me in any way.
– pjneary
Nov 12 at 14:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an app running in its own app pool that writes often to a text log file. When the 29-hour fixed recycle hits, I usually run into problems because the new process is launched while the old process is still there. This causes "can't open the file because it's in use by another process."
First I simply tried to set Disable Overlapped Cycle to True, but that takes my service off the air for 90 seconds or more, which is not good. If I reduce the Shutdown Time Limit I suppose it would help, but I'd prefer not to kill the process immediately.
I tried setting up an Application_End handler in Global.asax, but I find it's almost never getting called because (I think) my process is still running a thread somewhere (possibly the logging process).
I tried Application_Disposed but so far it does not get called when IIS kills the process after the shutdown time expires, and it's probably too late by then if I go back to overlapped recycle, which I think is preferable.
What I need is a signal from IIS that says "Hey, I want you to stop," that lets me close the log file immediately, but that does not seem to exist.
Any suggestions would be appreciated.
iis application-pool recycling
I have an app running in its own app pool that writes often to a text log file. When the 29-hour fixed recycle hits, I usually run into problems because the new process is launched while the old process is still there. This causes "can't open the file because it's in use by another process."
First I simply tried to set Disable Overlapped Cycle to True, but that takes my service off the air for 90 seconds or more, which is not good. If I reduce the Shutdown Time Limit I suppose it would help, but I'd prefer not to kill the process immediately.
I tried setting up an Application_End handler in Global.asax, but I find it's almost never getting called because (I think) my process is still running a thread somewhere (possibly the logging process).
I tried Application_Disposed but so far it does not get called when IIS kills the process after the shutdown time expires, and it's probably too late by then if I go back to overlapped recycle, which I think is preferable.
What I need is a signal from IIS that says "Hey, I want you to stop," that lets me close the log file immediately, but that does not seem to exist.
Any suggestions would be appreciated.
iis application-pool recycling
iis application-pool recycling
asked Nov 8 at 21:56
pjneary
95158
95158
1
IIS won't shut down your web app (callingApplication_End) until the new web app is running, and that's how overlapped recycle works under the hood. Thus, you should not expect IIS to give you the "signal". Usually it should be your web app that informs of the other copies of itself a new copy is launched by IIS. Or the new copy of your web app implements some simple retry mechanism to write to the file (as once IIS shuts down the old copy of the app, the file is writable).
– Lex Li
Nov 9 at 0:08
I know I won't get Application_End until IIS is really going to end my app--although even that call seems to not happen most of the time. I was hoping there was some other call. Frankly, adding some kind of code to hunt down which W3WP my old app is running in and setting up some IPC channel to communicate with it seems a whole lot of work. At this point I'd consider shutting of recycle completely--it's not helping me in any way.
– pjneary
Nov 12 at 14:59
add a comment |
1
IIS won't shut down your web app (callingApplication_End) until the new web app is running, and that's how overlapped recycle works under the hood. Thus, you should not expect IIS to give you the "signal". Usually it should be your web app that informs of the other copies of itself a new copy is launched by IIS. Or the new copy of your web app implements some simple retry mechanism to write to the file (as once IIS shuts down the old copy of the app, the file is writable).
– Lex Li
Nov 9 at 0:08
I know I won't get Application_End until IIS is really going to end my app--although even that call seems to not happen most of the time. I was hoping there was some other call. Frankly, adding some kind of code to hunt down which W3WP my old app is running in and setting up some IPC channel to communicate with it seems a whole lot of work. At this point I'd consider shutting of recycle completely--it's not helping me in any way.
– pjneary
Nov 12 at 14:59
1
1
IIS won't shut down your web app (calling
Application_End) until the new web app is running, and that's how overlapped recycle works under the hood. Thus, you should not expect IIS to give you the "signal". Usually it should be your web app that informs of the other copies of itself a new copy is launched by IIS. Or the new copy of your web app implements some simple retry mechanism to write to the file (as once IIS shuts down the old copy of the app, the file is writable).– Lex Li
Nov 9 at 0:08
IIS won't shut down your web app (calling
Application_End) until the new web app is running, and that's how overlapped recycle works under the hood. Thus, you should not expect IIS to give you the "signal". Usually it should be your web app that informs of the other copies of itself a new copy is launched by IIS. Or the new copy of your web app implements some simple retry mechanism to write to the file (as once IIS shuts down the old copy of the app, the file is writable).– Lex Li
Nov 9 at 0:08
I know I won't get Application_End until IIS is really going to end my app--although even that call seems to not happen most of the time. I was hoping there was some other call. Frankly, adding some kind of code to hunt down which W3WP my old app is running in and setting up some IPC channel to communicate with it seems a whole lot of work. At this point I'd consider shutting of recycle completely--it's not helping me in any way.
– pjneary
Nov 12 at 14:59
I know I won't get Application_End until IIS is really going to end my app--although even that call seems to not happen most of the time. I was hoping there was some other call. Frankly, adding some kind of code to hunt down which W3WP my old app is running in and setting up some IPC channel to communicate with it seems a whole lot of work. At this point I'd consider shutting of recycle completely--it's not helping me in any way.
– pjneary
Nov 12 at 14:59
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
The Problem is in your App Itself and not in IIS.
Just Disposed the object after you calling or writing into text file to avoid this issue.
I've been encountered that same way before. I also writing to a text file for a log then error occurs "can't open the file because it's in use by another process." but when i disposed the object not just the word disposed, you have to check it carefully for a proper disposal.
Its all gone and run the app normally.
Hope it helps
Might work in some scenarios, but you generally can't open and close the file every time you write something on a production server, this will be too slow. There should be some special handling for overlapping scenario, or cache and write infrequently and retry.
– Brian Clink
Nov 9 at 16:47
My experience is writing a log so often but i did not encounter any error again so far. I am also deploying the project in server side like IIS. On my first time of it, if feels me annoying("can't open the file because it's in use by another process.) but when i modify and check the code, fortunately its working well tell now. Almost 5 years now since then all of my projects are working well so how can i say that it is a bug or something in IIS side. The solution you have to do temporarily is to remove it inTask Managerbut that is not a better solution for this.
– Vijunav Vastivch
Nov 12 at 0:30
I'm am using a custom log class that is used across several services in my product, both web and Windows services. In fact I do cache and write about every 30 seconds by default, but in some of the busier services, I can be writing millions of lines over the course of a few hours, so I'm leery of closing/disposing the stream objects. At this point I'm considering either turning off the recycle completely, or making the shutdown time very short and picking a recycle time of 2:00AM rather than the 29 hour.
– pjneary
Nov 12 at 16:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The Problem is in your App Itself and not in IIS.
Just Disposed the object after you calling or writing into text file to avoid this issue.
I've been encountered that same way before. I also writing to a text file for a log then error occurs "can't open the file because it's in use by another process." but when i disposed the object not just the word disposed, you have to check it carefully for a proper disposal.
Its all gone and run the app normally.
Hope it helps
Might work in some scenarios, but you generally can't open and close the file every time you write something on a production server, this will be too slow. There should be some special handling for overlapping scenario, or cache and write infrequently and retry.
– Brian Clink
Nov 9 at 16:47
My experience is writing a log so often but i did not encounter any error again so far. I am also deploying the project in server side like IIS. On my first time of it, if feels me annoying("can't open the file because it's in use by another process.) but when i modify and check the code, fortunately its working well tell now. Almost 5 years now since then all of my projects are working well so how can i say that it is a bug or something in IIS side. The solution you have to do temporarily is to remove it inTask Managerbut that is not a better solution for this.
– Vijunav Vastivch
Nov 12 at 0:30
I'm am using a custom log class that is used across several services in my product, both web and Windows services. In fact I do cache and write about every 30 seconds by default, but in some of the busier services, I can be writing millions of lines over the course of a few hours, so I'm leery of closing/disposing the stream objects. At this point I'm considering either turning off the recycle completely, or making the shutdown time very short and picking a recycle time of 2:00AM rather than the 29 hour.
– pjneary
Nov 12 at 16:02
add a comment |
up vote
0
down vote
The Problem is in your App Itself and not in IIS.
Just Disposed the object after you calling or writing into text file to avoid this issue.
I've been encountered that same way before. I also writing to a text file for a log then error occurs "can't open the file because it's in use by another process." but when i disposed the object not just the word disposed, you have to check it carefully for a proper disposal.
Its all gone and run the app normally.
Hope it helps
Might work in some scenarios, but you generally can't open and close the file every time you write something on a production server, this will be too slow. There should be some special handling for overlapping scenario, or cache and write infrequently and retry.
– Brian Clink
Nov 9 at 16:47
My experience is writing a log so often but i did not encounter any error again so far. I am also deploying the project in server side like IIS. On my first time of it, if feels me annoying("can't open the file because it's in use by another process.) but when i modify and check the code, fortunately its working well tell now. Almost 5 years now since then all of my projects are working well so how can i say that it is a bug or something in IIS side. The solution you have to do temporarily is to remove it inTask Managerbut that is not a better solution for this.
– Vijunav Vastivch
Nov 12 at 0:30
I'm am using a custom log class that is used across several services in my product, both web and Windows services. In fact I do cache and write about every 30 seconds by default, but in some of the busier services, I can be writing millions of lines over the course of a few hours, so I'm leery of closing/disposing the stream objects. At this point I'm considering either turning off the recycle completely, or making the shutdown time very short and picking a recycle time of 2:00AM rather than the 29 hour.
– pjneary
Nov 12 at 16:02
add a comment |
up vote
0
down vote
up vote
0
down vote
The Problem is in your App Itself and not in IIS.
Just Disposed the object after you calling or writing into text file to avoid this issue.
I've been encountered that same way before. I also writing to a text file for a log then error occurs "can't open the file because it's in use by another process." but when i disposed the object not just the word disposed, you have to check it carefully for a proper disposal.
Its all gone and run the app normally.
Hope it helps
The Problem is in your App Itself and not in IIS.
Just Disposed the object after you calling or writing into text file to avoid this issue.
I've been encountered that same way before. I also writing to a text file for a log then error occurs "can't open the file because it's in use by another process." but when i disposed the object not just the word disposed, you have to check it carefully for a proper disposal.
Its all gone and run the app normally.
Hope it helps
answered Nov 9 at 8:56
Vijunav Vastivch
2,9041720
2,9041720
Might work in some scenarios, but you generally can't open and close the file every time you write something on a production server, this will be too slow. There should be some special handling for overlapping scenario, or cache and write infrequently and retry.
– Brian Clink
Nov 9 at 16:47
My experience is writing a log so often but i did not encounter any error again so far. I am also deploying the project in server side like IIS. On my first time of it, if feels me annoying("can't open the file because it's in use by another process.) but when i modify and check the code, fortunately its working well tell now. Almost 5 years now since then all of my projects are working well so how can i say that it is a bug or something in IIS side. The solution you have to do temporarily is to remove it inTask Managerbut that is not a better solution for this.
– Vijunav Vastivch
Nov 12 at 0:30
I'm am using a custom log class that is used across several services in my product, both web and Windows services. In fact I do cache and write about every 30 seconds by default, but in some of the busier services, I can be writing millions of lines over the course of a few hours, so I'm leery of closing/disposing the stream objects. At this point I'm considering either turning off the recycle completely, or making the shutdown time very short and picking a recycle time of 2:00AM rather than the 29 hour.
– pjneary
Nov 12 at 16:02
add a comment |
Might work in some scenarios, but you generally can't open and close the file every time you write something on a production server, this will be too slow. There should be some special handling for overlapping scenario, or cache and write infrequently and retry.
– Brian Clink
Nov 9 at 16:47
My experience is writing a log so often but i did not encounter any error again so far. I am also deploying the project in server side like IIS. On my first time of it, if feels me annoying("can't open the file because it's in use by another process.) but when i modify and check the code, fortunately its working well tell now. Almost 5 years now since then all of my projects are working well so how can i say that it is a bug or something in IIS side. The solution you have to do temporarily is to remove it inTask Managerbut that is not a better solution for this.
– Vijunav Vastivch
Nov 12 at 0:30
I'm am using a custom log class that is used across several services in my product, both web and Windows services. In fact I do cache and write about every 30 seconds by default, but in some of the busier services, I can be writing millions of lines over the course of a few hours, so I'm leery of closing/disposing the stream objects. At this point I'm considering either turning off the recycle completely, or making the shutdown time very short and picking a recycle time of 2:00AM rather than the 29 hour.
– pjneary
Nov 12 at 16:02
Might work in some scenarios, but you generally can't open and close the file every time you write something on a production server, this will be too slow. There should be some special handling for overlapping scenario, or cache and write infrequently and retry.
– Brian Clink
Nov 9 at 16:47
Might work in some scenarios, but you generally can't open and close the file every time you write something on a production server, this will be too slow. There should be some special handling for overlapping scenario, or cache and write infrequently and retry.
– Brian Clink
Nov 9 at 16:47
My experience is writing a log so often but i did not encounter any error again so far. I am also deploying the project in server side like IIS. On my first time of it, if feels me annoying(
"can't open the file because it's in use by another process.) but when i modify and check the code, fortunately its working well tell now. Almost 5 years now since then all of my projects are working well so how can i say that it is a bug or something in IIS side. The solution you have to do temporarily is to remove it in Task Manager but that is not a better solution for this.– Vijunav Vastivch
Nov 12 at 0:30
My experience is writing a log so often but i did not encounter any error again so far. I am also deploying the project in server side like IIS. On my first time of it, if feels me annoying(
"can't open the file because it's in use by another process.) but when i modify and check the code, fortunately its working well tell now. Almost 5 years now since then all of my projects are working well so how can i say that it is a bug or something in IIS side. The solution you have to do temporarily is to remove it in Task Manager but that is not a better solution for this.– Vijunav Vastivch
Nov 12 at 0:30
I'm am using a custom log class that is used across several services in my product, both web and Windows services. In fact I do cache and write about every 30 seconds by default, but in some of the busier services, I can be writing millions of lines over the course of a few hours, so I'm leery of closing/disposing the stream objects. At this point I'm considering either turning off the recycle completely, or making the shutdown time very short and picking a recycle time of 2:00AM rather than the 29 hour.
– pjneary
Nov 12 at 16:02
I'm am using a custom log class that is used across several services in my product, both web and Windows services. In fact I do cache and write about every 30 seconds by default, but in some of the busier services, I can be writing millions of lines over the course of a few hours, so I'm leery of closing/disposing the stream objects. At this point I'm considering either turning off the recycle completely, or making the shutdown time very short and picking a recycle time of 2:00AM rather than the 29 hour.
– pjneary
Nov 12 at 16:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216768%2fhow-to-close-a-file-during-app-pool-recycle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
IIS won't shut down your web app (calling
Application_End) until the new web app is running, and that's how overlapped recycle works under the hood. Thus, you should not expect IIS to give you the "signal". Usually it should be your web app that informs of the other copies of itself a new copy is launched by IIS. Or the new copy of your web app implements some simple retry mechanism to write to the file (as once IIS shuts down the old copy of the app, the file is writable).– Lex Li
Nov 9 at 0:08
I know I won't get Application_End until IIS is really going to end my app--although even that call seems to not happen most of the time. I was hoping there was some other call. Frankly, adding some kind of code to hunt down which W3WP my old app is running in and setting up some IPC channel to communicate with it seems a whole lot of work. At this point I'd consider shutting of recycle completely--it's not helping me in any way.
– pjneary
Nov 12 at 14:59