I wrote a functional test reproducing this problem in xwiki-platform-scheduler-test-docker (SchedulerIT#verifyJobURLsAreBasedOnWikiDescriptor), and while doing so it turned out that the condition to trigger it is narrower than what this description says.
Scheduling from a real HTTP request is not affected
Scheduling a job from the Scheduler UI (the "Schedule" action) does not reproduce the problem: HttpServletRequestStub#daemon defaults to true, and XWikiServletRequestStub(XWikiRequest) only copies the flag when the source request is itself a stub. The stub that SchedulerPlugin#prepareJobStubContext builds out of a real servlet request is therefore already a daemon one, and the job generates its URLs from the wiki descriptor, as expected.
The problem needs the request of the scheduling context to already be a non daemon stub, i.e. a context restored by RequestInitializer#restoreRequest from a stored request URL: that is the only place setting daemon to false, and it does so as soon as the stored context contains request.url or request.base.
Note that the restorations going through JobRequestContext (XAR import from the administration, WikiInitializerJob, jobs started over REST) build the request with the XWikiServletRequestStub(URL, String, Map) constructor, which leaves daemon at its default true: those are not affected either. The contexts restored through XWikiContextContextStore are the ones concerned: asynchronous rendering when the rendered content declares request.url or request.base among its context entries, the PDF export job, AbstractAsyncClassDocumentInitializer and the asynchronous event store.
The test
it sets the server property of the main wiki descriptor to a host that no test request can have, so that a URL generated from the descriptor can be told apart from a URL generated from a captured request (xwiki.home is not set in the tested instance, otherwise it would take precedence and hide the problem)
it creates a Groovy job saving xwiki.getDocument('Main.WebHome').getExternalURL() in a page, and leaves it unscheduled
it schedules the job from an asynchronously rendered script – an async macro with context="wiki, user, request.url" calling $xwiki.scheduler.scheduleJobs(...) – which is where the job context, and thus the URL factory the job uses, is built
it triggers the job from the Scheduler UI and checks the host of the URL the job generated
Result
The test fails on master (18.9.0-SNAPSHOT):
org.opentest4j.AssertionFailedError: The job generated the URL [http://host.testcontainers.internal:8080/xwiki/bin/view/Main/], based on the HTTP request captured when it was scheduled instead of the wiki descriptor ==> expected: <scheduler.xwiki.test> but was: <host.testcontainers.internal>
and it passes with the dummy.setDaemon(true) proposed in this description, the rest of the Scheduler test suite staying green.
This message was sent by Atlassian Jira (v9.3.0#930000-sha1:287aeb6)
If image attachments aren't displayed, see this article.