<div dir="ltr">I have observed that on_load handlers happen late in the init process.<div><br></div><div>I would have expected that an on_load handler ran when the module is loaded into the VM <a href="https://github.com/erlang/otp/blob/1c8187a1dd5b2c807caf73cc86657c5eae0f6569/erts/preloaded/src/init.erl#L895">as primLoads are processed</a>.</div><div><br></div><div>But, it turns out they do not run at that point. Before on load handlers run, all applications are loaded, and kernel started. Only later does the hook get executed, and boot continues starting the rest of applications.</div><div><br></div><div>In a production Distilley release for example (embedded mode, in particular), this is easy to see passing ERL_OPTS=-init_debug to a command. You'll see {`running_on_load_handler`, Module} printed when the hook is invoked.</div><div><br></div><div>Two questions here:</div><div><br></div><div>1) Is this done because starting kernel is needed to have a minimum of things setup in order to run code, do I/O etc. in the on_load handler? If yes, is application:load/1 carefully written to not depend on such setup? (since it runs before kernel starts).</div><div><br></div><div>2) On the other hand, if you load a module on demand in interactive mode, the hook is executed right away. That says primLoad has to load modules in a different way somehow. Do you know which is the difference?</div><div><br></div></div>