1

I am creating an AI competition where competitors can upload their AIs (node servers) to our node server and we take in their code and run it in an automated fashion.

On first code upload, I use child_process.spawn() to run node server.js xxx where xxx is their port. I store the spawned child as well as the competitor id in memory. When they upload a second time, I search through my stored child processes by competitor id and call process.kill(pid, 'SIGKILL') on the process.

On each spawned child I have set up child.on('close', callback). On this callback, I want to delete their code directory where I uploaded their node server to and then copy in their new code and spin up a node server again. However, I cannot delete the code directory because it is still in use (I get an EBUSY error), I think I am not killing the node process correctly.

Is this an okay approach? If so, what am I doing wrong?

ZWand19
  • 113
  • 1
  • 8
  • Oh boy, executing user-submitted code... :D I hope you're restricting their access somehow, wouldn't want them setting your box on fire. Regarding your question, have you tried catching the exit event instead of close? – Jephron May 27 '14 at 23:14
  • Well the competition is internal to my company so we are kind of assuming people are nice :) plus its running on its own vm. but anyways, yeah I tried both and neither worked – ZWand19 May 28 '14 at 01:13
  • Off the top of my head the hackiest solution I can come up with is to set a timeout of a few seconds to give it some time to quit. There's probably a better way though. Can you see if the process is actually being killed? – Jephron May 28 '14 at 01:20
  • yes it is being killed, no longer shows up in the process list. not sure why I can't delete it... friend told me to look into node-supervisor instead of deleting/restarting node process – ZWand19 May 28 '14 at 01:30
  • Yeah, a module will probably make your life easier. I seem to remember having some issues controlling child processes in Node. Still a lot less of a drag than Perl, though :D Obviously something is still locking up the directory. Either it's the child process, in which case your 'close' event is firing before it's finished with the filesystem, or it's something in your controller that's accessing the directory. Hard to say. Anyway, hope you figure it out. – Jephron May 28 '14 at 01:40

0 Answers0