Action called twice only in debug mode

I have an action that reads from files, then inserts rows into a database table and after that redirects to a index of model (index.php?r=punch)

when I try to run the action by entering the url in the browser (index.php?r=punch/read-files), data is read from the files and inserted into db correctly, but the same action is running again which generates duplicates in the database. I have noticed that when I comment the below lines, which is disabling debug mode, my code works as expected inserting the rows just once. But I really need debug mode to work properly… any ideas?

//defined(‘YII_DEBUG’) or define(‘YII_DEBUG’, true);
//defined(‘YII_ENV’) or define(‘YII_ENV’, ‘dev’);

Can you try disabling debug extension without disabling debug mode and switching to production environment?

I commented the below in web.php

// configuration adjustments for ‘dev’ environment
//$config[‘bootstrap’][] = ‘debug’;
//$config[‘modules’][‘debug’] = [
//‘class’ => ‘yii\debug\Module’,
// uncomment the following to add your IP if you are not connecting from localhost.
//‘allowedIPs’ => [‘127.0.0.1’, ‘::1’, ‘192.168.166.72’],
//];

and removed the below comments in index.php

// comment out the following two lines when deployed to production
defined(‘YII_DEBUG’) or define(‘YII_DEBUG’, true);
defined(‘YII_ENV’) or define(‘YII_ENV’, ‘dev’);

this works (meaning no double calls) but with no debug toolbar

Anyone having the same issue?

Don’t do this part. I’ve asked for disabling debug toolbar but not disabling debug mode.

I did, but its bit weird. If I use my fiber optic connection, I occasionally get my actions (create/update/delete) fired twice. the second one will produce an error since the row is added/changed/deleted already.

However, when I switched to other provider or using 4G connection, I did not encounter this problem.

Yes, I have debugging toolbar active and still in debug mode, not production mode. Do these have any effects?

It sounds like something is requesting page twice and since when debug toolbar is disabled there’s no such behavior it sounds like a bug in debug toolbar. Still, it’s very strange that it wasn’t reported by anyone so either it’s environment specific or very hard to reproduce. Anyway, without a way to reproduce it we can’t fix it…

Other people already encountered this issue before, that’s how I knew it was the debug toolbar. Check the link below

In my environment I can reproduce it every time. It’s a LAN application, not on the WWW. I will try just commenting the below and report again.

defined(‘YII_ENV’) or define(‘YII_ENV’, ‘dev’);

I got it again this morning. It called twice when delete when debug toolbar was not allowed (IP was not put on the list when called twice). But I think debug toolbar was active?

FYI, I used yii2-ajaxcrud extension, and the call was ajax call not pjax call.

The thing is this was happened when I used my fiber optic connection. It worked fine using 4G connection. This is the some piece of code on the same server access from the same computer.

The below configuration does not result in double calls.

commented the below web.php

// configuration adjustments for ‘dev’ environment
//$config[‘bootstrap’][] = ‘debug’;
//$config[‘modules’][‘debug’] = [
//‘class’ => ‘yii\debug\Module’,
// uncomment the following to add your IP if you are not connecting from localhost.
//‘allowedIPs’ => [‘127.0.0.1’, ‘::1’, ‘192.168.166.72’],
//];

debug mode is enabled in index.php

// comment out the following two lines when deployed to production
defined(‘YII_DEBUG’) or define(‘YII_DEBUG’, true);
defined(‘YII_ENV’) or define(‘YII_ENV’, ‘dev’);

this works (meaning no double calls) but with no debug toolbar

P.S.: I can reproduce the bug any time

Can you reproduce it without yii2-ajaxcrud?

I dont have any problem without yii2-ajaxcrud. Even, the weird thing is that, when the it is in the index action, I have no problem. The problem was come up when the gridview is put on the view action. In my example, I have Header with Details.In header/index no problem. however ini header/view where I put detailview of header and gridview of details. The ajax calls are twice only when I used fiber optic connection. 4g connection to the same code will have no problems.

I am thinking that if it is slow connection, it will fail and call twice. but it is ajax call not pjax call…

That’s likely a problem in yii2-ajaxcrud. Worth checking with the author of that extension.

I’ m using new theme for frontend and tree menu not work find then remove js/jquery.js it work find. but it be will other problem that the action will be twice called and will be ok if uncommented js/jquery.js .
public $js = [
‘js/jquery.js’, //If remove, action will be twice
‘js/plugins.js’,
‘js/functions.js’,

This cases are happening now and then. In the last two months it happened once. I suspected the provider is not having the public IP (Use some NAT mechanism) or some routing problem. We can still browsing the web but somehow AJAX calls failed, csrf problem?. I implemented the HTTPS server to replace HTTP server, and it seemed the problem disappear. Hopefully, this is the final solution of my problem.

Can someone help me to verify that this situation is correct?

TIA,

Daniel