0

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.

pjneary
  • 1,136
  • 6
  • 8
  • 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 09 '18 at 00: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 '18 at 14:59

1 Answers1

1

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

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
  • 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 09 '18 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 '18 at 00: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 '18 at 16:02