R.NET can not get instance at server

Dec 11, 2014 at 1:43 PM
Edited Dec 11, 2014 at 8:14 PM
Hi R.NET expects,

I have a problem with R in my C# .NET web application project. R.NET works perfectly in my test machine, but when I move my application to server, my application is stucking at line "engine = REngine.GetInstance();" and is loading forever.

Server OS: Windows Server 2008 R2
R version: R-3.1.2
R.NET version: 1.5.16
framework: .NET Framework 4.5
web server: IIS7
test machine OS: Windows 7
Visual Studio 2012

I configure my web application for any CPU (32bit or 64bit). I added "C:\Program Files\R\R-3.1.2\bin\x64;C:\Program Files\R\R-3.1.2\bin\i386" to system variable PATH, as suggested in other posts.

Here is my Global.asax file, where I want the R.NET to be initialized.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using RDotNet;
using RDotNet.NativeLibrary;

namespace WebSite
{
    public class Global : System.Web.HttpApplication
    {
        public static REngine engine;

        protected void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup

            // set up enviromental path for R.dll      
            REngine.SetEnvironmentVariables();

            engine = REngine.GetInstance(); // this is the line where my web application got stuck

            // Initializes settings
            engine.Initialize();

            engine.Evaluate("library(Cairo);");
        }

        protected void Session_Start(object sender, EventArgs e)
        {
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
        }

        protected void Session_End(object sender, EventArgs e)
        {
        }

        protected void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
            engine.Close();
            engine.Dispose();
        }
    }
}
Could anyone be so kind to tell me why?? It works perfectly for local test! Anything I miss to do? Many thanks!
Dec 14, 2014 at 11:01 PM
It seems that debugging the process might be useful here. I can't really guess as to why it is hanging like that.
Dec 15, 2014 at 12:57 PM
Edited Dec 15, 2014 at 2:56 PM
Thanks a lot for the tip!!! I do not know how to debug an ASP.NET application on IIS yet, but will try to learn that asap.
Dec 16, 2014 at 9:29 AM
Edited Dec 16, 2014 at 10:24 AM
To test if it is a general problem with R.NET working with IIS7 under windows server 2008 R2. I created a default web form application by visual studio, just add these three lines to Application_Start in Global.asax file:
REngine.SetEnvironmentVariables();
engine = REngine.GetInstance(); // this is the line where my web application got stuck
engine.Initialize();
The problem remains the same: the application runs successfully under visual studio build, but will fail under IIS. I debugged the IIS process and found out the line is stucking on RDotNet.REngine, method Initialize, line GetFunction<setup_Rmainloop>()();. There is no error returned, the web page is simply loading forever.

I compared trace between visual studio build and IIS processing, after the GetFunction<setup_Rmainloop>()() get the function name successfully, visual studio process will go on to Callback function in RDotNet.Devices.CharacterDeniceAdapter, while IIS process just stuck here.

I have added the R path to system path but it makes no differences (as r.net can successfully find Rhome and R path). Anyone have any ideas? It will save my day!!!
Dec 16, 2014 at 9:54 PM
I'll try to take a look at it on Thurs or Fri to see if I can replicate the issue.
Dec 17, 2014 at 8:00 AM
skyguy94 wrote:
I'll try to take a look at it on Thurs or Fri to see if I can replicate the issue.
Oh thank you so much skyguy94!!! I look forward to hear from it!
Jan 4, 2015 at 7:57 PM
First off, I'm very sorry for the long delay. The holidays, conferences, and work tasks got the better of me.

The hang at setup_Rmainloop typical happens during an attempt at an engine re-init. My first suspicion about your code and issue is the reason it works fine under IIS Express, but when you move it to IIS is that you have an issue with your AppPool recycling. Of course, that doesn't make much sense since the app pool is supposed to be a process boundary, but I wonder if that's the only application you have running using R.Net in that AppPool? Of course, I don't understand why I can replicate it on my local machine. I'm going to spin up an ec2 instance and see what I can make happen there.

I'm going to keep looking at it. I don't know where you are located, or if you do conferences much, but I'll be at codemash next week if you want to collaborate. Perhaps we skype sometime tues or weds and you can demo the issue? I'm very intrigued by it (my poor response not withstanding).

Blue Skies,
Ritch
Jan 4, 2015 at 8:12 PM
Oh, and if you wanted to open this as in issue on github, I'd really appreciate it. Codeplex is killing me.
Jan 8, 2015 at 10:28 AM
Thank you so much Ritch! It is really nice of you, I am very moved! I also replied so late due to long holidays and getting sick after long holidays, so don't you feel sorry.

I am in Germany so probably will miss the conference, pity. But if you have some time to kill, skype might be a fine idea.

So back to the topic, that web application is the only application in my AppPool. So I don't know if it has anything to do with AppPool recycling. I used to restart Rengine inside my web application, then I got a clear error message saying Rengine cannot be reinitialized. But this time there is no error but just hanging.

I am actually surprised to hear that you could not replicate on your machine. Maybe there is just some dumb mistake I did not notice (if so it will make everything so much easier).

I have opened an issue on github here if you are interested: https://github.com/EcologyRocks/TestRWebsite/issues/1

So thanks again! Look forward to hear from you!

Cheers,
Amy
Jan 23, 2015 at 4:52 PM