Skip to content

Resolving InstallationId writes to disk by default #3607

Closed
@bitsandfoxes

Description

@bitsandfoxes

Problem

Writing to disk on restricted platforms - i.e. Nintento Switch - causes the game to crash: getsentry/sentry-unity#1804

What's happening

We're fetching the installationId lazily

_lazyInstallationId = new(() => new InstallationIdHelper(this).TryGetInstallationId());

and during event enrichment that installationId gets resolved

eventLike.User.Id ??= _options.InstallationId;

and this causes the InstallationIdHelper to attempt to create a persistent installationId on disk

var id =
TryGetPersistentInstallationId() ??
TryGetHardwareInstallationId() ??
GetMachineNameInstallationId();

private string? TryGetPersistentInstallationId()
{
try
{
var rootPath = options.CacheDirectoryPath ??
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var directoryPath = Path.Combine(rootPath, "Sentry", options.Dsn!.GetHashString());
Directory.CreateDirectory(directoryPath);

Solutions

  1. Change the accessor for InstallationId from internal to public. That way the Unity SDK can overwrite the lazy initialization during options construction. (Hacky)
    internal string? InstallationId => _lazyInstallationId.Value;
  2. Add controls over writing to disk. That could be a new option. Or we reuse CacheDirectoryPath. With the CacheDirectoryPath set to null we skip all operations writing to disk. We've already got FileSystem in place.
    public void CreateDirectory(string path) => Directory.CreateDirectory(path);

    We could replace all instances of the SDK directly calling Directory.Create for File.Write going through FileSystem and make that adhere to the options.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions