How to stop a running php process on heroku server? - php

I have written a slack auto-message service on heroku that will auto send a message on some event. However, I have accidentally made an infinity loop and it keeps sending error message to my slack account. (the loop caused by retrying on error and I forgot to add a counter on that)
I have tried restarting the server by typing the command heroku restart (at that directory, so app name can be omitted), as well as git pushing my corrected version which should restart the server. I even turn off the server by settings 0 dymo to it. None of these works and I still keep receiving message on slack.
I am quite sure I have turn off my heroku server, so I think there should be other way to stop the process. The php process will have a bash_exec to trigger phantomjs. Not sure if it is related to the current problem. Do anyone has any suggestions?
P.S. As request, this is my code with important information hidden.
<?php
sendSlackMessageToChannel("Request received. Loading, please wait.");
startPhantomJS();
function startPhantomJS() {
$return_value = bash_exec("path/to/phantomjs myscript.js");
sendSlackMessageToMyself($return_value);
if ($return_value == "error")
startPhantomJS();
else
sendSlackMessageToChannel($return_value);
}

Related

XMPP (XMPPHP) session won't start

Fellows I'm working in a new server and, at first, it looks all good. The eJabberd webadmin runs OK and I was able to even create an user by that interface.
The situation is, the same application that usually ran on my previous server freezes at the waiting for the session to start, the code:
$this->lnk->processUntil('session_start');
The $this->lnk->connect(); works fine but it seems that the session can't be set. Any suggestions for where or what I should go take a look first?
ACKs:
The XMPP application has been set the same way than was in the older server.
Here is the whole code:
$this->lnk = new XMPPHP_XMPP($this->config['host'],
$this->config['port'],
$this->config['username'],
$this->config['password'],
$this->config['service'],
$this->config['domain'],
$printlog = false,
$loglevel = XMPPHP_Log::LEVEL_VERBOSE);
$this->lnk->useEncryption(true);
$this->lnk->connect();
$this->lnk->processUntil('session_start');
The issue was caused by $this->lnk->useEncryption(true);. Since my new server hadn't proper SSL/TLS settings, this line caused the freezing of the code.
Possible solvings are disabling encryption and adjusting you SSL/TLS credentials.

Php Serial to Arduino on linux openwrt

I'm trying to get a web page based serial communication working with an Arduino which is connected to a router running Openwrt, it does work but only when either screen is running or remotely connected via putty, for some reason the php to serial is not starting a session properly? I use ser2net to manage the serial with the following setting
1000:raw:0:/dev/ttyACM0:9600
I have also tried stty with various settings advised on this forum
Update 1
also tried 80:raw:0:/dev/ttyACM0:9600 as setting for ser2net assuming port 80 is web/http port
my php code is
if (isset($_GET['action']))
{
$serial = new phpSerial();
$serial->deviceSet('/dev/ttyACM0');
$serial->confBaudRate(9600);
$serial->confParity('none');
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl('none');
$serial->deviceOpen();
if ($_GET['action'] == "PIN_12_HIGH")
{
$serial->sendMessage("A");
}
if ($_GET['action'] == "PIN_12_LOW")
{
$serial->sendMessage("B");
}
if ($_GET['action'] == "PIN_11_HIGH")
{
$serial->sendMessage("C");
}
if ($_GET['action'] == "PIN_11_LOW")
{
$serial->sendMessage("D");
}
$serial->deviceClose();
}
update 2
if i add sleep(1); to my php code it seems to solve some of the problems
a few points
adding sleep to php code allows the commands to reach the arduino after a reset caused by the serial connection, but if i turn on led 1 when i turn led 2 on led 1 gets reset to off. so this would mean a more complicated code in the form of logging an array of choices and sending the whole updated array to arduino, i would like to avoid this and just send 1 command at a time.
again when screen or putty are active the arduino does not have this reset problem? so the question is what does screen and putty do to keep the connection alive that stty and php serial does'nt?
Update 3
After finding this helpful post i seem to have discovered a possible solution with no reset hacking needed to the Arduino.
Adding cat /dev/ttyACM0 & to the start up config of the Openwrt router enables serial communication with the Arduino without reseting it on every transmission of data.
problems left:
I think this code is binding arduino to a session with the router? But im unsure.
It seems to be eating the return value from the Arduino stopping my php page getting the data return?
My System Log file contains the missing returned data from Arduino?
Update 4
As i needed to get this working, i used a capacitor to stop the auto reset from serial communication on the arduino.
As stated screen and putty dont create this reset problem when communicating with arduino, i tried many different settings with STTY but couldnt find a solution , but i am still trying and keen to discover how screen/putty does this.
It looks like your PHP is attempting to directly connect to the tty. Where the ser2net is likely already connected to the tty. Only one application at a time can be connected to the tty. Either stop the ser2net or your PHP should connect to the tcp listener of the desired tty as per your ser2net.conf

Extension php_win32service installs the service, but the service won't start up (hangs)

I am trying to setup a PHP web application on a Windows Server 2008 SP1 machine. We have this application running correctly on a Windows Server 2003, but are now unable to get the Windows services built with php_win32service PHP extension starting up correctly.
Here are the relevant sections of the PHP code. Here is the section that does the install and uninstall:
if ($argv[1] == 'install') {
win32_create_service(array(
'service' => $service_name,
'display' => $service_name,
'params' => '"'. __FILE__ . '"' . ' run'));
exit;
}
else if ($argv[1] == 'uninstall') {
win32_delete_service($service_name);
exit;
}
else if ($argv[1] != 'run') {
die("Invalid arguments");
}
win32_start_service_ctrl_dispatcher($service_name);
And here is the main loop:
while (1) {
switch (win32_get_last_control_message()) {
case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_SERVICE_RUNNING); break; // Respond with status
case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
}
processQueue();
usleep(500000);
}
The install and uninstall work as long as I run cmd as administrator. If I launch cmd under the account that I use to login to the server then the service is not created win32_create_service. I can also run the script with the run parameter from the command line and it runs correctly, but when I try start the service it just hangs on with the Starting message and never goes into Started state.
I think this issue has something to do with the rights of the LocalSystem account on the machine, but I do not know what rights are needed to get this to work properly. I also do not know how I can debug this and find out what error/issue is occurring with the WIN32_SERVICE_CONTROL_INTERROGATE, especially as I do not have rights on this server to make changes to security settings. Any changes that I need made to security settings I need to communicate to the network administrator so that he can perform the changes. Can anyone offer any help with debugging or resolving this issue?
UPDATE:
This issue only seems to occur in the 64-bit version of PHP. It seems that the 64-bit compile of php_win32service runs into some sort of problem when trying to start the service. I removed the 64-bit versions of PHP and 'php_win32service' and replaced them with the 32-bit versions. The service then started correctly.
Have you tried with NetworkService account? Other way is creating own service account. Do you communicate outside the server? Maybe firewall?
Ps. the network service account has a default password (system known)
Note that this account does not have a password, so any password information that you provide in this call is ignored.
http://msdn.microsoft.com/nl-nl/library/windows/desktop/ms684272(v=vs.85).aspx
Updated Answer:
I am now convinced that this issue arises when you update or downgrade PHP to a different version, but fail to remove and recreate the services using the php_win32service for the newly installed version of PHP. This was mentioned as an aside in my old answer, but after encountering the same thing on another server and resolving it by removing and recreating the service entries, I am convinced that this is the true cause of this issue and that is not a bug in PHP.
Old Answer:
I encountered this issue again when I tried to update PHP on a Windows Server 2008 machine from 5.3 to 5.6. From the testing that I have done I have concluded that php_win32service.dll extension works correctly for PHP 5.3, but not from 5.4 upwards. From 5.4 there seems to be a bug whereby the function win32_get_last_control_message always returns 0 rather than a valid status.
I finally got a clue to what was going on from a post made over here:
http://enlinea.creaelicita.cl/guia/function.win32-set-service-status.html
So, if you have code such as this:
while (1) {
switch (win32_get_last_control_message()) {
case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_SERVICE_RUNNING); break; // Respond with status
case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
}
usleep(3000000);
// Main script goes here
processQueue();
}
then
win32_set_service_status(WIN32_SERVICE_RUNNING);
will never get called, because win32_get_last_control_message never gets the value of 4 (WIN32_SERVICE_CONTROL_INTERROGATE).
A workaround which I use is to add a default case to the switch statement that sets the service status. Like this:
switch (win32_get_last_control_message()) {
case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_SERVICE_RUNNING); break; // Respond with status
case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
default: win32_set_service_status(WIN32_SERVICE_RUNNING);
}
As an aside, I also found that this issue occurred if I upgraded to PHP 5.4 and did not recreate the service entries before trying to start them. So besides applying the above workaround, remember to first uninstall the windows service in question and reinstall it from your PHP script.

Problem with named pipes breaking php/windows

I have a named pipe being read by a python program, installed as a service in Windows 2008 to print data from a database. The code (php) that writes to the pipe is as follows. ($cmd is the command line passed).
$pipeName = "\\\\.\\pipe\\printerpipe";
#trigger_error("");
#$pipe = fopen($pipeName,'rb+');
$a = error_get_last();
if ($a['message'] == '') {
fwrite($pipe,$cmd);
$ans = fread($pipe,256);
} else {
print error message "service not loaded"
}
There is no custom error handler.
This has worked fine on my test machine (Window7) and on the production server - until today. Today it printed three reports, then reported the service not loaded error, and then printed 4 times more! I doubt that the system was busy or overloaded - I have one user, the report is tiny (most reports are one page), and the printing is spooled. Most reports appear on the printer as the reply to the request is being rendered by the browser.
Also the link between my Python code and the metworked printer appers to break every night, so i have to restart the service every morning. Can someone confirm that this is due to the printer being switched off, or suggest how to re-establish the link, other than restarting the printing service?
I need to make the service reliable, and I don't know how to proceed. Ideas and advice gratefully recieved.
It could be this code - I don't understand the error handling, and the code was copied from a post on the internet.

session_start hangs

since a few hours our server hangs every time you do a session_start.
For testing purposes i created a script which looks like this:
<?php
session_start();
?>
Calling it from the console hangs and it can't even be stopped with ctrl-c, only kill -9 works. The same for calling it via Apache. /var/lib/php/session/ stays empty but permissions are absolutely fine, www can write and also has read permissions for all parent folders.
According to the admins there were no changes made on the server and there is no special code registered for sessions. The Server is CentOS 4 or 5 and yesterday everything was working perfectly. We rebooted the server and updated PHP, but nothing changed.
I've ran out of ideas, any suggestions?
UPDATE
We solved this problem by moving the project to another server, so while the problem still exists on one server there is no immediate need for a solution anymore.
I will keep the question open in case someone has an idea for others having a similar problem in the future, though.
There are many reasons for that, here are a few of them:
A. The session file could be opened exclusively.
When the file lock is not released properly for whatever reason, it is causing session_start() to hang infinitely on any future script executions.
Workaround: use session_set_save_handler() and make sure the write function uses fopen($file, 'w') instead of fopen($file, 'x')
B. Never use the following in your php.ini file (entropie file to "/dev/random"), this will cause your session_start() to hang:
<?php
ini_set("session.entropy_file", "/dev/random");
ini_set("session.entropy_length", "512");
?>
C.
session_start() needs a directory to write to.
You can get Apache plus PHP running in a normal user account. Apache will then of course have to listen to an other port than 80 (for instance, 8080).
Be sure to do the following things:
- create a temporary directory PREFIX/tmp
- put php.ini in PREFIX/lib
- edit php.ini and set session.save_path to the directory you just created
Otherwise, your scripts will seem to 'hang' on session_start().
If this helps:
In my scenario, session_start() was hanging at the same time I was using the XDebug debugger within PHPStorm, the IDE, on Windows. I found that there was a clear cause: Whenever I killed the debug session from within PHPStorm, the next time I tried to run a debug session, session_start() would hang.
The solution, if this is your scenario, is to make sure to restart Apache every time you kill an XDebug session within your IDE.
I had a weird issue with this myself.
I am using CentOS 5.5x64, PHP 5.2.10-1. A clean ANSI file in the root with nothing other than session_start() was hanging. The session was being written to disk and no errors were being thrown. It just hung.
I tried everything suggested by Thariama, and checked PHP compile settings etc.
My Fix:
yum reinstall php; /etc/init.d/httpd restart
Hope this helps someone.
To everyone complaining about the 30 seconds of downtime being unacceptable, this was an inexplicable issue on a brand new, clean OS install, NOT a running production machine. This solution should NOT be used in a production environment.
Ok I face the same problem on 2 PC, 1 is MAC mini XAMPP, 1 is Windows 10 Xampp.
Both is php spent infinity to run session_start(). Both PHP version is 7.x.x
I found that session files is lock to read and write. So that I added code to make PHP read session files and immediately unlock when done with
<?php
session_start([
'read_and_close' => true,
]);
?>
or
<?php
//For PHP 5.x
session_start();
session_write_close();
?>
After this PHP unlock session file => Problems solve
The problem: -
Iv experienced (and fixed) the problem where file based sessions hang the request, and database based sessions get out of sync by storing out of date session data (like storing each session save in the wrong order).
This is caused by any subsequent request that loads a session (simultaneous requests), like ajax, video embed where the video file is delivered via php script, dynamic resource file (like script or css) delivered via php script, etc.
In file based sessions file locking prevents session writing thus causing a deadlock between the simultaneous request threads.
In database based session the last request thread to complete becomes the most recent save, so for example a video delivery script will complete long after the page request and overwrite the since updated session with old session data.
The fix: -
If your ajax or resource delivery script doesnt need to use sessions then easiest to just remove session usage from it.
Otherwise you'd best make yourself a coffee and do the following: -
Write or employ a session handler (if not already doing so) as per http://www.php.net//manual/en/class.sessionhandler.php (many other examples available via google search).
In your session handler function write() prepend the code ...
// processes may declare their session as read only ...
if(!empty($_SESSION['no_session_write'])) {
unset($_SESSION['no_session_write']);
return true;
}
In your ajax or resource delivery php script add the code (after the session is started) ...
$_SESSION['no_session_write'] = true;
I realise this seems like a lot of stuffing around for what should be a tiny fix, but unfortunately if you need to have simultaneous requests each loading a session then it is required.
NOTE if your ajax or resource delivery script does actually need to write/save data, then you need to do it somewhere other than in the session, like database.
Just put session_write_close(); befor Session_start();
as below:
<?php
session_write_close();
session_start();
.....
?>
I don't know why, but changing this value in /etc/php/7.4/apache2/php.ini worked for me:
;session.save_path = "/var/lib/php/sessions"
session.save_path = "/tmp"
To throw another answer into the mix for those going bananas, I had a session_start() dying only in particular cases and scripts. The reason my session was dying was ultimately because I was storing a lot of data in them after a particularly intensive script, and ultimately the call to session_start() was exhausting the 'memory_limit' setting in php.ini.
After increasing 'memory_limit', those session_start() calls no longer killed my script.
For me, the problem seemed to originate from SeLinux. The needed command was chcon -R -t httpd_sys_content_t [www directory] to give access to the right directory.
See https://askubuntu.com/questions/451922/apache-access-denied-because-search-permissions-are-missing
If you use pgAdmin 4 this can happen as well.
If you have File > Preferences > SQL Editor > Options > "Auto Commit" disabled, and you just ran a query using the query tool but didn't manually commit, then session_start() will freeze.
Enable auto commit, or manually commit, or just close pgAdmin, and it will no longer freeze.
In my case it seems like it was the NFS Share that was locking the session , after restarting the NFS server and only enabled 1 node of web clients the sessions worked normally .
Yet another few cents that might help someone. In my case I was storing in $_SESSION complex data with several different class objects in them and session_start() couldn't handle the whole unserialization as not every class was loaded on session_start. The solution is my case was to serialize/jsonify data before saving it into the $_SESSION and reversing the process after I got the data out of session.

Categories