Editing programs “while they are running”? How?

后端 未结 8 2138
夕颜
夕颜 2021-02-13 06:19

This question is a corollary to: Editing programs “while they are running”? Why?

I\'m only recently being exposed to the world of Clojure and am fascinated by a few exam

相关标签:
8条回答
  • 2021-02-13 07:14

    Yes, it's also possible in other languages. I've done it in Python for an online server.

    The key feature needed is the ability to define or redefine new functions and methods at runtime and this is easy with Python where you have "eval", "exec" and where classes and modules are first-class objects that can be patched at runtime.

    I implemented it practically by allowing a separate socket connection (for security reasons only from the local machine) accepting strings and exec-ing them in the context of the running server. Using this approach I was able to update the server while it was running without having the connected users to suffer a disconnection. The server was composed of two processes and was an online playfield with a client written in Haxe/Flash, using a permanent socket connection for realtime interaction between players.

    In my case I used this possibility only for some quick fixes (the biggest was removing ghost connections that were remaining up in case of a network disconnect in a specific protocol state and I also fixed the bug that allowed the creation of these ghost connections).

    I also used this management backdoor to get some resource use information while the server was running. As a funny note the very first bug I fixed on a running server was a bug in the backdoor machinery itself (but it wasn't online with real users in that case, just artificial users for load testing, so it was more like a check if it could be done than a real use as there would have been no problems at all shutting down the server for that).

    IMO the bad part of doing this kind of live hacking is that once you fix the running instance and you can be sure that the fix works, you still have to do it in the regular source code and if the fix isn't trivial you cannot be 100% sure that the fix will work once you boot an updated version of the server.

    Even if your environment allows to save the patched image without bringing it down, still you cannot be sure that the fixed image will start or will work correctly. The "fix" on the running program could for example break the startup process making it impossible to get to a correct running state.

    0 讨论(0)
  • 2021-02-13 07:15

    It's possible in many languages, but only if you have the following features:

    • Some form of REPL or similar so you can interact with the running environment
    • Some form of namespace that can be modified at runtime
    • Dynamic binding against the namespace, so that if you change items in the namespace then referring code automatically picks up the change

    Lisp/Clojure has all of these built in by default, which is one of the reasons why it is particularly prominent in the Lisp world.

    Example demonstrating these features (all at the Clojure REPL):

    ; define something in the current namespace
    (def y 1)
    
    ; define a function which refers to y in the current namespace
    (def foo [x] (+ x y))
    
    (foo 10)
    => 11
    
    ; redefine y
    (def y 5)
    
    ; prove that the change was picked up dynamically
    (foo 10)
    => 15
    
    0 讨论(0)
提交回复
热议问题