Zend certified PHP/Magento developer

Programmatically update Stock Status Magento 2

I am trying to update Stock Status when the quantity is updated. Either from admin or web api (rest).

app/code/Namespace/Module/etc/events.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="Namespace_Module::Auto_Update_Qty" instance="NamespaceModuleObserverProductSaveAfter"/>
    </event>
</config>

app/code/Namespace/Module/Observer/ProductSaveAfter.php

public function execute(Observer $observer)
    {
        try
        {
            $product = $observer->getProduct();
            if ($product->getTypeId() != 'configurable')
            {
                $sku = $product->getSku();
                $stockItem = $this->stockRegistry->getStockItemBySku($sku);
                $qty = $stockItem->getQty();

                $stockItem->setQty($qty);
                $stockItem->setIsInStock((bool)$qty);
                $this->stockRegistry->updateStockItemBySku($sku, $stockItem);
            }
        }
        catch (Exception $e)
        {
            return $e->getMessage();
        }
    }

The event is being fired correctly and i see in the table cataloginventory_stock_item that is_in_stock column, briefly is set correct, but then return to original state.

I have disabled all third party modules, but still the same behavior.

All MSI modules are enabled.

I have created a debug log file in cataloginventory_stock_item_save_after, and this seems to show correct data. Below i am setting to “Not in stock”, but is_in_stock resets to original data.

2022-04-07T15:04:57+00:00 INFO (6): Array
(
    [item_id] => 1288
    [product_id] => 1886
    [stock_id] => 1
    [qty] => 11
    [min_qty] => 0
    [use_config_min_qty] => 1
    [is_qty_decimal] => 0
    [backorders] => 0
    [use_config_backorders] => 1
    [min_sale_qty] => 1
    [use_config_min_sale_qty] => 1
    [max_sale_qty] => 10000
    [use_config_max_sale_qty] => 1
    [is_in_stock] => 0
    [notify_stock_qty] => 1
    [use_config_notify_stock_qty] => 1
    [manage_stock] => 1
    [use_config_manage_stock] => 0
    [stock_status_changed_auto] => 0
    [use_config_qty_increments] => 1
    [qty_increments] => 1
    [use_config_enable_qty_inc] => 1
    [enable_qty_increments] => 0
    [is_decimal_divided] => 0
    [website_id] => 0
    [type_id] => simple
    [min_qty_allowed_in_shopping_cart] => Array
        (
            [0] => Array
                (
                    [customer_group_id] => 32000
                    [min_sale_qty] =>
                    [record_id] => 0
                )

        )

)

Why is is_in_stock not being updated correctly like i want? Why does it keeps going back to original state?