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.










share|improve this question


















  • 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















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.










share|improve this question


















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 21:56









pjneary

95158




95158








  • 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














  • 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








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












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






share|improve this answer





















  • 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












  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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






share|improve this answer





















  • 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












  • 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















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






share|improve this answer





















  • 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












  • 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













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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • 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












  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings