Question Authors' Forum

Running C# in CodeRunner

Picture of Richard Lobb
Re: Running C# in CodeRunner
by Richard Lobb - Sunday, 21 January 2018, 9:11 PM

Hi Martin

Thanks for posting. Just to answer your question about why the | e('py') is necessary....

When wrapping a raw string from the author form or student answer in quotes to make a literal string within the template code, you generally need to escape various special characters that might result in invalid string literals. As a trivial example, if a student's answer was "Hi" (including the double quotes) and you wrote, in JavaScript.


you'd get

s = ""Hi"";

which is broken. Hence Twig provides a range of escapers, so that for a JavaScript template you can write

s = "{{STUDENT_ANSWER | e('js')}}"

which would give you the valid

s = "\"Hi\"";

I've added to the Twig base set of escapers, e('py'), e('java') and e('matlab'). The Java escaper can be used for C and C++ too. 

The Python escaper is intended for use only within triple-quoted string literals. It escapes only double quotes (in case the raw string itself has triple quotes in it) and backslashes. Backslashes need to be escaped so that, for example, a 2-character \n in the student's code doesn't get converted to a single newline character in the triple-quoted literal.


Picture of Martin Zwerschke
Re: Running C# in CodeRunner
by Martin Zwerschke - Friday, 30 November 2018, 11:50 PM

Hi Richard,

we are using C#  under coderunner using "mono" and the c#-via python prototype for a while now - works like a charme.

Now  I tried to use C# 7 features (like the "switch-case" pattern match with keyword "when")

mcs C# compiler is called in the prototype. But it does not understand "when" keyword.

I tried to add option -langversion:7.2  (which mcs can handle) but it still does not support "when"

So i tried to switch to  newer  "csc"  compiler , that can handle "when" and other C#7 features.

I used:

return_code =['csc', '/unsafe+', '/nologo', '-langversion:latest', 'prog.cs'])

for the compilation. It seems to work.

But the code execution runs into runtime exceptions:

"Unhandled Exception:

[ERROR] FATAL UNHANDLED EXCEPTION: System.OutOfMemoryException: Out of memory
at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal.AllocHGlobal(intptr)"


But if i use 

mono ./test.exe

to execute the csc-generated compilation on the jobe server's command line, the program executes without problems though.

Does jobe block the code?

Picture of Richard Lobb
Re: Running C# in CodeRunner
by Richard Lobb - Saturday, 1 December 2018, 9:19 AM

Jobe sets resource limits (processes, cpu time, memory, file output) on all tasks. It seems you have hit the default limit on memory, presumably because the newer C# version wants more memory than the older one. With this question type, which runs C# in a subprocess, you need enough memory for both Python and C# together. Depending on what version you're running, the default is somewhere between 300 MB and 500 MB as I recall.

Easily fixed. In your prototype, open the Advanced Customisation panel and experiment with the setting of the Memory Limit field. A value of 0 turns off the memory limit checking altogether so you may want to try this first, but that's not recommended as a permanent setting.

Picture of Martin Zwerschke
Re: Running C# in CodeRunner
by Martin Zwerschke - Tuesday, 4 December 2018, 11:06 AM

Increasing the memory limit did the trick.

I was suprised, however, that the csc compiler needs so much memory.

I had to increase to at least 600MB to succeed and now use a limit of 650MB for my C#-questions prototype.

Of course it is not only csc that uses the memory but the whole jobe toolchain.

At least csc needs much more memory than mcs does.

(Perhaps because it comes from Microsoft...)

Thanks for your help.