According to: tutorial/WolframSystemSessions
Initialization
On startup, the Wolfram Language kernel does the following:
Performs license management operations.
Runs Wolfram Language commands specified in any -run options passed to the kernel executable.
Runs the Wolfram Language commands in the systemwide initialization file
$BaseDirectory/Kernel/init.m
.Runs the Wolfram Language commands in the user-specific initialization file
$UserBaseDirectory/Kernel/init.m
.Loads
init.m
andKernel/init.m
files in Autoload directories.Begins running the main loop.
So I'd say that packages in Autoload have a freedom to do everything since everything should be already loaded. Yet this example fails:
dir = FileNameJoin[{$UserBaseDirectory, "Autoload", "Fetch", "Kernel"}];
CreateDirectory[dir, CreateIntermediateDirectories -> True]
SetDirectory @ dir;
Export[
"init.m",
"Print @ StringTake[URLFetch[\"www.wolfram.com\"], 300]; ",
"Text"]
Now quit the Kernel and evaluate something. I only get messages:
URLFetch::invhttp: Couldn't resolve proxy name.
StringTake[$Failed, 300]
Throw::nocatch: Uncaught Throw[False] returned to top level.
Throw::nocatch: Uncaught Throw[False] returned to top level.
Throw::nocatch: Uncaught Throw[False] returned to top level.
General::stop: Further output of Throw::nocatch will be suppressed during this calculation.
Break::nofwd: No enclosing For, While, or Do found for Break[].
While normally this procedure works StringTake[URLFetch["www.wolfram.com"], 300]
.
Question What's the problem? What more do we need to know about the Initialization stack? Are there any workarounds?
Currently I'm just setting procedure via ScheduledTask
to fire 2 seconds later. But that's just silly.
This is not a question about URLFetch
(yet good to know this specific problem) but about things I have to know to not be surprised next time.
Answer
Update
Leaving my original answer below for historical purposes, however it only applies up until version 11.1.1.
As of version 11.2.0, the kernel startup initialization has been overhauled and this example (as well as others) now works correctly: placing the URLFetch
call in init.m
does result in an output like
Persistence & Initialization framework, and I recommend Roman Maeder's presentation from the 2017 Wolfram Technology Conference.
To make my comment into an answer,
The basic problem, which is not so easy to fix, is that the kernel initialization code has protection against aborts. Since Throw
/Catch
are implemented internally using this (C code level) abort machinery, they are not functional during initialization, so running any code using them is problematic.
The URLFetch
implementation does depend on Catch
working correctly, as can be seen from
TracePrint[URLFetch["www.wolfram.com"], _Catch]
Some related questions are the following: (1), (2), (3), (4).
Comments
Post a Comment