Skip to content

Fix AppInstance key reregistration issue#6

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-5
Draft

Fix AppInstance key reregistration issue#6
Copilot wants to merge 3 commits intomainfrom
copilot/fix-5

Conversation

Copy link

Copilot AI commented May 22, 2025

Issue

When an app instance unregisters its key using AppInstance.UnregisterKey() and then calls AppInstance.FindOrRegisterKey() with the same key again, it appears to "successfully" register, but the key is actually invalid (returns an empty string). This means that when a subsequent process calls AppInstance.FindOrRegisterKey() with the same key, it will incorrectly return null instead of finding the correct instance.

Root Cause

The issue was in the SharedMemory::IsValid() method, which only checked if the name exists (m_name.size() != 0), but didn't verify if the underlying memory view is valid:

bool IsValid()
{
    return (m_name.size() != 0);
}

After calling UnregisterKey(), the m_key.Reset() method would clear the memory view and file handle, but if the name was somehow preserved, IsValid() would incorrectly return true, causing the key validation to fail in unexpected ways.

Fix

Modified the SharedMemory::IsValid() method to check both that the name exists AND that the memory view is valid:

bool IsValid()
{
    return (m_name.size() != 0) && (m_view != nullptr);
}

Testing

Added a new test case specifically for the unregister and re-register scenario to verify the fix works correctly:

TEST_METHOD(UnregisterAndReRegisterKey)
{
    // First register a key
    auto instance = AppInstance::FindOrRegisterForKey(c_testKeyName);
    VERIFY_IS_NOT_NULL(instance);
    VERIFY_IS_TRUE(instance.IsCurrent());
    VERIFY_ARE_EQUAL(instance.Key(), c_testKeyName);

    // Unregister the key
    instance.UnregisterKey();
    VERIFY_ARE_EQUAL(instance.Key(), L"");

    // Re-register the same key
    auto reRegisteredInstance = AppInstance::FindOrRegisterForKey(c_testKeyName);
    VERIFY_IS_NOT_NULL(reRegisteredInstance);
    VERIFY_IS_TRUE(reRegisteredInstance.IsCurrent());
    VERIFY_ARE_EQUAL(reRegisteredInstance.Key(), c_testKeyName);
}

This fix ensures that when an app instance unregisters its key, subsequent calls to FindOrRegisterKey with the same key will properly register the key again, allowing the instance to be found correctly by other processes.

Fixes #5.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits May 22, 2025 06:02
Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
Copilot AI changed the title [WIP] Re-registering AppInstance causes Key to be invalid Fix AppInstance key reregistration issue May 22, 2025
Copilot AI requested a review from MuyuanMS May 22, 2025 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Re-registering AppInstance causes Key to be invalid

2 participants