-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement client watchdog #4081
Draft
apple502j
wants to merge
1
commit into
FabricMC:1.21.2
Choose a base branch
from
apple502j:crash-report-info/client-watchdog
base: 1.21.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−1
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
version = getSubprojectVersion(project) | ||
|
||
moduleDependencies(project, [':fabric-lifecycle-events-v1']) |
86 changes: 86 additions & 0 deletions
86
...-v1/src/client/java/net/fabricmc/fabric/impl/client/crash/report/info/ClientWatchdog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.impl.client.crash.report.info; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Locale; | ||
|
||
import com.mojang.logging.LogUtils; | ||
import org.slf4j.Logger; | ||
|
||
import net.minecraft.Bootstrap; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.server.dedicated.DedicatedServerWatchdog; | ||
import net.minecraft.util.Util; | ||
import net.minecraft.util.crash.CrashReport; | ||
import net.minecraft.util.crash.ReportType; | ||
|
||
import net.fabricmc.api.ClientModInitializer; | ||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; | ||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; | ||
import net.fabricmc.fabric.mixin.client.crash.report.info.MinecraftClientAccessor; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
|
||
public class ClientWatchdog implements ClientModInitializer { | ||
private static final Logger LOGGER = LogUtils.getLogger(); | ||
private static final int DEFAULT_MAX_TIME_MS = 30000; | ||
private static final boolean ENABLED = FabricLoader.getInstance().isDevelopmentEnvironment() || Boolean.getBoolean("fabric.clientWatchdog.enabled"); | ||
private static final int MAX_TIME_MS = Integer.getInteger("fabric.clientWatchdog.maxTimeMs", DEFAULT_MAX_TIME_MS); | ||
private volatile long tickStartTimeMs = -1; | ||
|
||
@Override | ||
public void onInitializeClient() { | ||
if (!ENABLED) return; | ||
ClientTickEvents.START_CLIENT_TICK.register((client) -> tickStartTimeMs = Util.getMeasuringTimeMs()); | ||
ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { | ||
Thread thread = new Thread(() -> run(client)); | ||
thread.setName("Fabric Client Watchdog"); | ||
thread.setDaemon(true); | ||
thread.start(); | ||
}); | ||
} | ||
|
||
public void run(MinecraftClient client) { | ||
while (client.isRunning()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there not be a sleep in this while loop, there is no need to constantly check the value. See: DedicatedServerWatchdog There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah, whoops. |
||
long tickStartTime = this.tickStartTimeMs; | ||
long currentTime = Util.getMeasuringTimeMs(); | ||
long deltaMs = currentTime - tickStartTime; | ||
|
||
if (tickStartTime >= 0 && deltaMs > MAX_TIME_MS) { | ||
LOGGER.error( | ||
LogUtils.FATAL_MARKER, | ||
"A single client tick took {} seconds (should be max {})", | ||
String.format(Locale.ROOT, "%.2f", (float) deltaMs / 1000), | ||
String.format(Locale.ROOT, "%.2f", (float) MAX_TIME_MS / 1000) | ||
); | ||
LOGGER.error(LogUtils.FATAL_MARKER, "Considering it to be crashed, client will forcibly shutdown."); | ||
CrashReport report = DedicatedServerWatchdog.createCrashReport("Fabric Client Watchdog", ((MinecraftClientAccessor) client).getThread().threadId()); | ||
client.addDetailsToCrashReport(report); | ||
Bootstrap.println("Crash report:\n" + report.asString(ReportType.MINECRAFT_CRASH_REPORT)); | ||
Path path = client.runDirectory.toPath().resolve("crash-reports").resolve("crash-" + Util.getFormattedCurrentTime() + "-client.txt"); | ||
|
||
if (report.writeToFile(path, ReportType.MINECRAFT_CRASH_REPORT)) { | ||
LOGGER.error("This crash report has been saved to: {}", path.toAbsolutePath()); | ||
} else { | ||
LOGGER.error("We were unable to save this crash report to disk."); | ||
} | ||
|
||
System.exit(1); | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ient/java/net/fabricmc/fabric/mixin/client/crash/report/info/MinecraftClientAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.mixin.client.crash.report.info; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
import net.minecraft.client.MinecraftClient; | ||
|
||
@Mixin(MinecraftClient.class) | ||
public interface MinecraftClientAccessor { | ||
@Accessor | ||
Thread getThread(); | ||
} |
11 changes: 11 additions & 0 deletions
11
...-crash-report-info-v1/src/client/resources/fabric-crash-report-info-v1.client.mixins.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"required": true, | ||
"package": "net.fabricmc.fabric.mixin.client.crash.report.info", | ||
"compatibilityLevel": "JAVA_21", | ||
"client": [ | ||
"MinecraftClientAccessor" | ||
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
really needs a testmod, id add a client command that sets a bool, and in the client ticket event just have a while (bool)